Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I love Nastya :)
- #include <iostream>
- using namespace std;
- struct LIST
- {
- int val;
- LIST * next;
- };
- LIST *read (LIST *head, int x)
- {
- LIST *pv =new LIST;
- pv->val=x;
- pv->next=head;
- head=pv;
- return head;
- }
- void print (LIST * list1)
- {
- for (LIST *a = list1; a; a = a->next) cout << a->val << " ";
- cout << "\n";
- }
- LIST * Sort (LIST *list)
- {
- LIST *reslist = NULL;
- LIST *next = NULL;
- for (LIST *a = list; a; a = next) {
- next = a->next;
- a->next = NULL;
- if (reslist==NULL) {
- reslist = a;
- }
- else if (reslist->val >= a->val) {
- a->next = reslist;
- reslist = a;
- }
- else {
- LIST* to = reslist;
- while ( !(to->next==NULL || to->next->val >= a->val ))
- to = to->next;
- a->next = to->next;
- to->next = a;
- }
- }
- return reslist;
- }
- LIST * delete_min(LIST *list) {
- int mi = 2*1000*1000*1000;
- for (LIST *a = list; a; a = a->next) {
- if (a->val < mi) mi = a->val;
- }
- if (list->val == mi) {
- LIST *ret = list->next;
- delete list;
- return ret;
- }
- bool deleted = false;
- for (LIST *a = list; !deleted && a->next; a = a->next) {
- LIST *s = a->next;
- if (s->val == mi) {
- a->next = s->next;
- delete s;
- deleted = true;
- }
- }
- return list;
- }
- int main ()
- {
- LIST * list1 = NULL;
- int n, data;
- cout << "Enter size of the list: ";
- cin >> n;
- for (int i=0; i<n; i++)
- {
- cin >> data;
- list1=read (list1, data);
- }
- print(list1);
- list1 = delete_min(list1);
- print(list1);
- list1 = Sort(list1);
- print(list1);
- list1 = delete_min(list1);
- print(list1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment