I will make a topic about Singly Link List. This method is important for Queue and Stack. Let me try the best for this tutorial.
We need 2 files such as,
1. .CPP (Main)
2. .h (Header)
1. main.cpp
#include <iostream> #include "list.h" using namespace std; int main() { list l; int menu; bool exit = false; l.first = nil; list *li = &l; while(exit==false) { cout<<"Menu :\n"; cout<<"1. insert first\n"; cout<<"2. insert last\n"; cout<<"3. delete first\n"; cout<<"4. delete last\n"; cout<<"5. view data\n"; cout<<"6. search\n"; cout<<"7. exit\n"; cout<<"Menu :";cin>>menu; if(menu==1){ address p = new elementList; cout<<"Input New Data :";cin>>info(p); p->next = nil; insertFirst(li,p); }else if(menu==2){ address p = new elementList; cout<<"Input Data :";cin>>info(p); p->next = nil; insertLast(li,p); }else if(menu==3){ deleteFirst(li); }else if(menu==4){ deleteLast(li); }else if(menu==5){ traversal(li); }else if(menu==6){ int x; cout<< "Input Data for Search : ";cin>>x; searching(li,x); }else if(menu==7){ exit = true; } }
return 0; }
2. List.h
#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #define nil NULL #define info(p) (p)->info #define next(p) (p)->next #define first(l) ((l).first) #define root(l) (l)->first #include <iostream> using namespace std; typedef struct elementList *address; struct elementList{ int info; address next; }; struct list{ address first; }; void insertFirst(list *l, address p) { next(p) = first(*l); //cout<<first(*l)<<endl; first(*l)= p; } void insertLast(list *l, address p) { if(first(*l)==nil){ insertFirst(l,p); }else{ address q = first(*l); while(next(q)!=nil){ q=next(q); } next(q)=p; } } void traversal(list *l) { address p = first(*l); if(first(*l)==nil){ cout<<"Empty List\n"; }else{ while(p!=nil){ cout<<info(p)<<endl; p = next(p); } } } void deleteFirst(list *l) { if(first(*l)==nil){ cout<<"Empty list\n"; }else{ address p =first(*l); first(*l)=next(p); next(p)=nil; delete(p); cout<<"First data has removed\n"; } } void deleteLast(list *l) { if(first(*l)==nil){ cout<<"empty list\n"; }else{ address prec; address p =first(*l); while(next(p)!=nil){ prec = p; p = next(p); } //cout<<next(prec); next(prec)=nil; delete(p); cout<< "Last data has removed\n"; } } void searching(list *l,int x) { address p = first(*l); bool found = false; int n=0; while(found == false && p!=nil) { if(info(p)==x){ found=true; } n++; p=next(p); } if(found==true){ cout<< "Data found at-"<<n<<endl; }else{ cout<< "NO data\n"; } }
We have 7 menus, insert first, insert last, delete first, delete last, view data, search, and exit.
In the Delete method, we need 2 variabel. First variabel for First(*L) and the second is next of address.
In the Insert method , we only need one variabel that is parameter input. So, the user will input data and we have made a procedure before for process this parameter.
View data is show your all data which you input such as first or last, this method will sort as you did before.
Search is for searching data what you want. It will show where is the location, first input, second, third , and so on.
I can't give you more. But i will answer your question.
I hope you like this tutorial. I can't speak romanian language. Thanks a lot #endif // LIST_H_INCLUDED