Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- struct item
- { int data;
- item *next;
- };
- item *head, *tail;
- bool isnull(void)
- { return (head == NULL);
- }
- void delete()
- { if(isnull())
- cout<<"Пустота"<<endl;
- else
- { item *p = head;
- head = head -> next;
- delete p;
- }
- }
- void add(int x)
- { item *p = new item;
- p -> data = x; p -> next = NULL;
- item *v = new item;
- item *p1 = new item;
- item *p2 = new item;
- int i = 0;
- if (isnull()) head = tail = p;
- else
- { p2 = head; p1 = head;
- while(p1 != NULL)
- { if (i == 1)
- { if(x <= p1 -> data )
- { v -> data = x; v -> next = p1;
- p2 -> next = v;
- return;
- }
- p2 = p2 -> next; }
- else
- { if(x <= p1 -> data )
- { v -> data = x; v -> next = p1;
- head = v;
- return;
- }
- }
- p1 = p1 -> next; i = 1;
- }
- if(p1 == NULL) { tail -> next = p; tail = p; }
- }
- }
- void out()
- { item *p = new item;
- cout <<endl;
- if(isnull())
- cout<<"Пустота"<<endl;
- else
- { cout<<"Очередь = ";
- p = head;
- int q=0;
- while(!isnull())
- {
- if (p != NULL)
- {cout << endl; cout<< q <<" элемент = "<<p->data<<";"; p = p -> next; q++; }
- }
- cout <<endl;
- cout <<endl;
- }
- }
- void delall() //Очистка очереди
- { while(!isnull())
- delete();
- }
- int main()
- { setlocale (LC_CTYPE, "Russian");
- int i = 1, num = 1, z;
- head = NULL;
- tail = NULL;
- while (num != 0)
- {cout<<" ------------------------------------"<<endl;
- cout<<"|--- 1 - добавить элемент -----------|"<<endl;
- cout<<"|--- 2 - извлечь элемент с начала ---|"<<endl;
- cout<<"|--- 3 - вывести элемент ------------|"<<endl;
- cout<<"|--- 4 - очистить очередь -----------|"<<endl;
- cout<<"|--- 0 - выход ----------------------|"<<endl;
- cout<<" ------------------------------------"<<endl;
- cout << endl;
- cout<<"Выберите действие ";
- cin>>num;
- switch(num)
- {
- case 1: cout<<"Введите элемент: ";
- cin>>z; add(z);
- break;
- case 2: delete(); break;
- case 3: out(); break;
- case 4: delall(); break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement