Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct List
- {
- int info;
- List *next;
- };
- List *First;
- List *Last;
- List *helper;
- void Create()
- {
- First=NULL;
- Last=NULL;
- }
- void insertFirst(int x)
- {
- helper=new List;
- helper->info=x;
- if(First==NULL)
- {
- First=helper;
- Last=helper;
- First->next=NULL;
- Last->next=NULL;
- }
- else
- {
- helper->next=First;
- First=helper;
- }
- }
- void insertLast(int x)
- {
- helper=new List;
- helper->info=x;
- helper->next=NULL;
- if(First==NULL)
- {
- First=helper;
- Last=helper;
- First->next=NULL;
- Last->next=NULL;
- }
- else
- {
- Last->next=helper;
- Last=helper;
- }
- }
- void insertAfter(int key, int x)
- {
- List *looker=new List;
- looker=First;
- helper=new List;
- helper->info=x;
- if(First==NULL)
- {
- cout<<"Error"<<endl;
- }
- else if(First==Last)
- {
- if(First->info!=key)
- {
- cout<<"Error"<<endl;
- }
- else
- {
- First->next=helper;
- Last=helper;
- Last->next=NULL;
- }
- }
- else
- {
- while(looker!=NULL)
- {
- if(looker->info==key)
- {
- helper->next=looker->next;
- looker->next=helper;
- break;
- }
- looker=looker->next;
- }
- }
- }
- void display()
- {
- List *helper=new List;
- helper=First;
- while(helper!=NULL)
- {
- cout<<helper->info<<" ";
- helper=helper->next;
- }
- cout<<"Wcisnij dowolny klawisz, aby kontynuowac"<<endl;
- cin.get();
- cin.get();
- }
- int main()
- {
- int choice=1000;;
- while(choice!=0)
- {
- cout<<"============================================="<<endl;
- cout<<"MENU"<<endl;
- cout<<"============================================="<<endl;
- cout<<"0. Wyjscie z programu"<<endl;
- cout<<"1. Tworzenie listy jednokierunkowej"<<endl;
- cout<<"2. Wstawianie elementu na poczatek listy"<<endl;
- cout<<"3. Wstawianie elementu na koniec listy"<<endl;
- cout<<"4. Wstawianie elementu po zadanym na liste"<<endl;
- cout<<"5. Wyswietlanie zawartosci listy"<<endl;
- cin>>choice;
- if(choice==1)
- {
- Create();
- }
- else if(choice==2)
- {
- cout<<"Podaj liczbe"<<endl;
- int x;
- cin>>x;
- insertFirst(x);
- }
- else if(choice==3)
- {
- cout<<"Podaj liczbe"<<endl;
- int x;
- cin>>x;
- insertLast(x);
- }
- else if(choice==4)
- {
- cout<<"Podaj element, po ktorym wstawisz"<<endl;
- int key;
- cin>>key;
- cout<<endl;
- cout<<"Podaj liczbe"<<endl;
- int x;
- cin>>x;
- insertAfter(key,x);
- }
- else if(choice==5)
- {
- display();
- }
- else if(choice==0)
- break;
- system("cls");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement