Created
April 2, 2015 04:16
-
-
Save Poplava/0365c36d89bf9c5f1011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
using namespace std; | |
typedef Node* pnode; | |
void addFirst(pnode &first, int x); | |
void addLast(pnode &last, int x); | |
void createList(pnode &first, ifstream &f, char* name); | |
void add(pnode &p, int x); | |
int deleteFirst(pnode &first); | |
int deleteNode(pnode &p); | |
void readList(pnode first); | |
pnode findMin(pnode first, int &min, bool &f); | |
void destructList(pnode &first); | |
struct Node | |
{ | |
int data; | |
pnode next; | |
}; | |
void addFirst(pnode &first, int x) | |
{ | |
pnode p; | |
p = new Node; | |
p->data = x; | |
p->next = nullptr; | |
first = p; | |
} | |
void addLast(pnode &last, int x) | |
{ | |
pnode p; | |
p = new Node; | |
p->data = x; | |
p->next = nullptr; | |
last->next = p; | |
last = p; | |
} | |
void createList(pnode &first, ifstream &f, char* name) | |
{ | |
f.open(name, ios::binary); | |
int x; | |
pnode p; | |
while (!f.eof()) { | |
f.read((char *)&x, sizeof x); | |
if (first == nullptr) { | |
addFirst(first, x); | |
p = first; | |
} | |
} | |
} | |
void add(pnode &p, int x) | |
{ | |
pnode q = new Node; | |
q->data = x; | |
q->next = p->next; | |
p->next = q; | |
p = q; | |
} | |
int deleteFirst(pnode &first) | |
{ | |
pnode p = first; | |
int x = p->data; | |
first = p->next; | |
delete p; | |
return x; | |
} | |
int deleteNode(pnode &p) | |
{ | |
int x; | |
pnode q = p->next; | |
p->next = q->next; | |
x = q->data; | |
delete q; | |
return x; | |
} | |
void readList(pnode first) | |
{ | |
pnode p = first; | |
while (p != nullptr) { | |
cout << p->data << ", "; | |
p = p->next; | |
} | |
cout << endl; | |
} | |
pnode findMin(pnode first, int &min, bool &f) | |
{ | |
pnode p = first, q = first; | |
int min = first->data; | |
f = false; | |
while (p->next != nullptr) { | |
if (p->next->data < min) { | |
q = p; | |
min = p->next->data; | |
f = true; | |
} | |
p = p->next; | |
} | |
return q; | |
} | |
void destructList(pnode &first) | |
{ | |
pnode p = first; | |
while (first != nullptr) { | |
first = first->next; | |
delete p; | |
} | |
} | |
int main() | |
{ | |
pnode first{ nullptr }, pmin; | |
int min; | |
bool f; | |
ifstream file; | |
char name[10]; | |
cin >> name; | |
createList(first, file, name); | |
readList(first); | |
pmin = findMin(first, min, f); | |
cout << endl << "min = " << min << endl; | |
if (!f) { | |
cout << deleteFirst(first) << endl; | |
} | |
else { | |
cout << deleteNode(pmin) << endl; | |
} | |
destructList(first); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment