Professional Documents
Culture Documents
Array Implementation of List Adt Aim: Alg: Program
Array Implementation of List Adt Aim: Alg: Program
h> void create(); void insert(); void deletion(); void search(); void display(); int a,b[20],n,d,e,f,i; void main() { int c; char g='y'; clrscr(); do { cout<<"\n Main Menu"; cout<<"\n 1.Create \n 2.Delete \n 3.Search \n 4.insert \n 5.Display \n 6.Exit"; cout<<"\n enter your choice\n"; cin>>c; switch(c) { case 1: create(); break; case 2: deletion(); break; case 3: search(); break; case 4: insert(); break; case 5: display(); break; case 6: exit(0); break; default: cout<<"The given number is not between 1-5\n"; } cout<<"\nDo u want to continue \n"; cin>>g; clrscr(); } while(g=='y'|| g=='Y'); getch(); }
void create() { cout<<"\n Enter the number\n"; cin>>n; for(i=0;i<n;i++) { cin>>b[i]; }} void deletion() { cout<<"Enter the limit u want to delete \n"; cin>>d; for(i=0;i<n;i++) { if(b[i]==d) { b[i]=0; }}} void search() { cout<<"Enter the limit \n"; cin>>e; for(i=0;i<n;i++) { if(b[i]==e) { cout<<"Value found the position\n"<<b[i]; }}} void insert() { cout<<"enter how many number u want to insert \n"; cin>>f; for(i=0;i<f;i++) { cin>>b[n++]; }} void display() { cout<<"\n\n\n"; for(i=0;i<n;i++) { cout<<"\n\n\n"<<b[i]; } }
1. Insert the element in the list. 2. Delete the element in the list 3. Forward traverse.
1. Dynamic data structure. 2. We can perform deletion and insertion any where in the list. 3. We can merge two list easily. Disadvantages of singly linked list:
1. Backward traversing is not possible in singly linked list. 2. Insertion is easy but deletion take some additional time, because disadvantage of backward traversing. Algorithm steps: Step1: Create nodes first,last,next,prev and cur then set the value as NULL.
Step 2: Read the list operation type. step 3: If operation type is create then process the following steps. 1. Allocate memory for node cur. 2. Read data in cur's data area. 3. Assign cur link as NULL. 4. Assign first=last=cur. Step 4: If operation type is Insert then process the following steps. 1. Allocate memory for node cur. 2. Read data in cur's data area. 3. Read the position the Data to be insert. 4. Availability of the position is true then assing cur's link as first and first=cur. 5. If availability of position is false then do following steps.
1. Assign next as cur and count as zero. 2. Repeat the following steps until count less than postion. 1 .Assign prev as next 2. Next as prev of link. 3. Add count by one. 4. If prev as NULL then display the message INVALID POSITION. 5. If prev not qual to NULL then do the following steps. 1. Assign cur's link as prev's link. 2. Assign prev's link as cur. Step5: If operation type is delete then do the following steps. 1. Read the position . 2. Check list is Empty .If it is true display the message List empty. 3. If position is first. 1. Assign cur as first. 2. Assign First as first of link. 3. Reallocate the cur from memory. 4. If position is last. 1. Move the current node to prev. 2. cur's link as Null. 3. Reallocate the Last from memory. 4. Assign last as cur. 5. If position is enter Mediate. 1. Move the cur to required postion. 2. Move the Previous to cur's previous position 3. Move the Next to cur's Next position. 4. Now Assign previous of link as next. 5. Reallocate the cur from memory.
step 6: If operation is traverse. 1. Assign current as first. 2. Repeat the following steps untill cur becomes NULL.
PROGRAM: /* single linked list */ #include<iostream.h> #include<conio.h> #include<stdlib.h> class list { struct node { int data; node *link; }*p; public: void inslast(int); void insbeg(int); void insnext(int,int); void delelement(int); void delbeg(); void dellast(); void disp(); int seek(int); list(){p=NULL;} ~list(); }; void list::inslast(int x) { node *q,*t; if(p==NULL) { p=new node; p->data=x; p->link=NULL; } else { q=p;
while(q->link!=NULL) q=q->link; t=new node; t->data=x; t->link=NULL; q->link=t; } cout<<"\n\nInserted successfully at the end.."; disp(); } void list:: insbeg(int x) { node *q; q=p; p=new node; p->data=x; p->link=q; cout<<"\n\nInserted successfully at the begining.."; disp(); }
void list::delelement(int x) { node *q,*r; q=p; if(q->data==x) { p=q->link; delete q; return; } r=q; while(q!=NULL) { if(q->data==x) { r->link=q->link; delete q; return; } r=q; q=q->link; } cout<<"\n\nElement you entered "<<x<<" is not found..";
} void list:: delbeg() { cout<<"\n\nThe list before deletion:"; disp(); node *q; q=p; if(q==NULL) { cout<<"\n\nNo data is present.."; return; } p=q->link; delete q; return; }
void list:: dellast() { cout<<"\n\nThe list before deletion:"; disp(); node *q,*t; q=p; if(q==NULL) { cout<<"\n\nThere is no data in the list.."; return; } if(q->link==NULL) { p=q->link; delete q; return; } while(q->link->link!=NULL) q=q->link; q->link=NULL; return; } list::~list() { node *q;
if(p==NULL) return; while(p!=NULL) { q=p->link; delete p; p=q; } } void list::disp() { node *q; q=p; if(q==NULL) { cout<<"\n\nNo data is in the list.."; return; } cout<<"\n\nThe items present in the list are \n"; while(q!=NULL) { cout<<q->data<<"\n"; q=q->link; } } void list :: insnext(int value,int position) { node *temp,*temp1; temp=p; if(temp1==NULL) { temp1= new node; temp1->data=value; temp1->link=NULL; p=temp1; return; } for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++) { if(i==(position-1)) { temp1= new node; temp1->data= value; temp1->link=temp->link; temp->link=temp1;
int list::seek(int value) { node *temp; temp=p; int position=0; while(temp!=NULL) { if(temp->data==value) return position+1; else { temp=temp->link; position=position+1; } } cout<<"\n\nElement "<<value<<" not found"; return 0; }
void main() { list l; int ch,v,p,ps; do { clrscr(); cout<<"\n\nOperations on List.."; cout<<"\n\n1.Insertion\n2.Deletion\n3.Display\n4.Seek\n5.Exit"; cout<<"\n\nEnter ur Option :"; cin>>ch; switch(ch) { case 1: clrscr(); cout<<"INSERTION"; cout<<"\n\n1.Insertion at begining\n2.Insertion at the end";
cout<<"\n3.Insertion between two Nodes"; cout<<"\n\nEnter ur choice:"; cin>>ps; cout<<"Enter the value to insert:"; cin>>v; switch(ps) { case 1: l.insbeg(v); break; case 2: l.inslast(v); break; case 3: cout<<"\nEnter the position to insert the value:"; cin>>p; l.insnext(v,p); break; default: cout<<"\nThe choice is invalid"; return; } break; case 2: clrscr(); cout<<"\n1.Delete the first element\n2.Delete the last element"; cout<<"\n3.Enter the element to delete from the list"; cout<<"\n\nEnter ur choice:"; cin>>ps; switch(ps) { case 1: l.delbeg(); cout<<"\nThe list after deletion:"; l.disp(); break; case 2: l.dellast(); cout<<"\nThe list after deletion:"; l.disp(); break; case 3: l.disp(); cout<<"\nEnter the element to delete : ";
cin>>v; l.delelement(v); cout<<"\nThe list after deletion:"; l.disp(); break; default: cout<<"\nThe option is invalid..."; break; } break; case 3: clrscr(); l.disp(); break; case 4: clrscr(); l.disp(); cout<<"\nEnter the element to search:"; cin>>v; cout<<"\nThe position of the element "<< v<<" is "<<l.seek(v); getch(); break; case 5: exit(1); default: cout<<"\nThe option is invalid..."; return; } getch(); }while(ch!=5); getch(); return; }
SAMPLE INPUT AND OUTPUT: SINGLY LINKED LIST 1.CREATE 2.INSERT 3.DELETE 4.EXIT ENTER YOUR CHOICE : 1 ENTER THE DATA: 10 10 1.CREATE 2.INSERT 3.DELETE 4.EXIT ENTER YOUR CHOICE : 2 ENTER THE DATA: 30 ENTER THE POSITION: 1 30 10 1.CREATE 2.INSERT 3.DELETE 4.EXIT ENTER YOUR CHOICE : 3 ENTER THE POSITION : 2 LIST IS EMPTY
READ THIS: Write aim,algorithm,program in obser. Other details for your ref Implementation Of Double Linked List
Definition: A variant of a linked list in which each item has a link to the previous item as well as the next. This allows easily accessing list items backward as well as forward and deleting any item in constant time. Also known as two-way linked list, symmetrically linked list. A doubly linked list is a linked list in which each node has three fields namely data field, forward link (Flink) and backward link (Blink). Flink points to the successor node in the list whereas Blink points to the predecessor node. Advantages:
1. Deletion process is easier 2. Additional pointer for previous node is not nessary. 3. Backwarding and forwarding traverse are possible.
Disadvantages : 1. More Memory space is required since it has two pointers. Alogrithm steps: Step 1: Read the list operation. Step 2: If operation is Create then process the following steps. 1. Create the new node and allocate memory for the new node. 2. Read the data in the new node. 3. Assign Flink and Blink as NULL. 4. Assign current as new. Step 3: If operation is Insertion do the following steps. i) Check insertion at beginning is true then
1. Create the new node and allocate memory for the new node 2. Read the data in the new node. 3. Move the current node to start position
4. Assign new->Blink =Null 5. Assign new->Flink=current 6. Now assign current->Blink=new ii) Insertion at between nodes is true then
1. Create the new node and allocate memory for the new node 2. Read the data in the new node. 3. Move the current node required position 4. Assign new nodes Blink=current. 5. Assign new nodes Flink=current->Flink 6. Assign current nodes Flink =new iii) Insertion at between nodes is true then
1. Create the new node and allocate memory for the new node. 2. Read the data in the new node. 3. Move the current node to end position. 4. Assign current nodes Flink =new. 5. Assign news Blink as current. 6. Assign news Flink as NULL. 7. Move end pointer to new node. Step 4: If operation is deletion then do the following steps. i). Check deletion at beginning is true then. 1. Move current pointer to start position. 2. Move the start pointer next node in the link. 3. Assign starts Blink as NULL. 4. Reallocate current node from memory.
1. Move the current pointer to required position. 2. Assign currents->Blink->Flink as currents Flink. 3. Assign currentss->Flink->Blink as currents Blink. 4. Reallocate current from memory. iii) Check deletion at end is true
1. Move the current pointer to end position. 2. Assign currents->Blink->Flink as Null. 3. Reallocate current from memory. 4. Reallocate current from memory. Step 5: If the operation is traversing. i) Check deletion at end is true
1. Move the current pointer to end position. 2. Repeat the following steps until current becomes NULL. 3. Display the data. 4. Current=current->Flink. ii) If Backward traversing then
1. Move the current to end position. 2. Repeat the following steps until current becomes NULL. 3. Display the data. 4. Current=current->Flink.
PROGRAM:
#include<iostream.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct list { int data; struct list*next; struct list *prev; }; typedef struct list node; node *find(node *,int); node *pre=NULL; node *fin=NULL; node *start; int main() { int menu(void); void create(node *); void display(node *); void insert(node *,int,int);
void reverse(node *); void del(node *,int); int choice,ch; int data,tar; node *newrec; start=NULL; clrscr(); do { choice=menu(); switch(choice) { case 1: cout<<"\nCreating the list"; cout<<"Enter the terms(type 0 to end)\n"; start=new node; create(start); break; case 2: if (start==NULL) cout<<"\nList does not exist"; else { cout<<"\nDisplaying the list\n";
display(start); } getch(); break; case 3: if (start==NULL) { cout<<"\nList does not exist"; getch(); } else { cout<<"\nDisplaying the list:"; reverse(fin); } break; case 4: if(start==NULL) { cout<<"\nList does not exist"; getch(); } else {
cout<<"\nEnter the term to insert:"; cin>>data; cout<<"\nEnter target term:"; cin>>tar; insert(start,data,tar); } break; case 5: if(start==NULL) { cout<<"\nlist does not exist:"; getch(); } else { cout<<"\nEnter the term to delete:"; cin>>data; del(start,data); } break; case 6: exit(0); default: cout<<"\nNot a valid choice"; }
}while(1); return(0); } int menu(void) { int ch; cout<<"\n1->Creation of the list"; cout<<"\n2->Displaying of the list"; cout<<"\n3->Reverse Displaying the list"; cout<<"\n4->Insertion of the list"; cout<<"\n5->Deletion of the list"; cout<<"\n6->Exit"; cout<<"\n\nEnter your choice:"; cin>>ch; return(ch); } void create(node *record) { cin>>record->data; if(record->data==0) { record->next=NULL; record->prev=pre; fin=record->prev;
pre=NULL; } else { record->prev=pre; record->next=new node; pre=record; create(record->next); } return; } void display(node *record) { if(record->next!=NULL) { cout<<record->data<<" "; display(record->next); } return; } void reverse(node *record) { if(record->prev!=NULL) {
cout<<record->data<<" "; reverse(record->prev); } return; } void insert(node *record,int data,int target) { node *tag,*newrec,*temp; newrec=new node; if(record->data==target) { newrec->next=record; newrec->prev=NULL; record->prev=newrec; start=newrec; } else { tag=find(record,target); temp=tag->prev; tag->prev=newrec; newrec->next=tag; temp->next=newrec; }
if(tag==NULL) { cout<<"Target item not present in the list\n"; getch(); return; } newrec->data=data; return; } void del(node *record,int target) { node *tag,*temp; if(record->data==target) { temp=record; start=start->next; start->prev=NULL; delete temp; } else { tag=find(record,target); if(tag->next->next==NULL) {
fin=fin->prev; fin->next=tag->next; } if(tag==NULL) { cout<<"Target item not present in the list\n"; getch(); return; } return; } } node *find(node *record,int target) { if(record->next->data==target) { return(record); } else if(record->next==NULL) return(NULL); else find(record->next,target); }
Sample Input Output: Main menu 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter your choice:1 Enter the data(-999 to stop): 5 10 15 -999 Main menu 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter your choice:2 New data item:20 Position of insertion: Main menu 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter your choice:4 Forward traversal 5 20 10 15 20
Backward traversal 20 15 10 20 5 Main menu 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter your choice:3 Enter the data to be deleted:5 Main menu 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter your choice:4 Forward traversal 20 10 15 20 Backward traversal 20 15 10 20
ii) If insertion at between any two nodes is true 1. 2. 3. 4. 5. Move the prev pointer to required position Create the new node and allocate memory for that node. Read the data in new nodes data part. Assign cur->link = prev-link (give the link to next node) Assign prev->link =cur.
1. 2. 3. 4. 5.
Move the prev is to last position ( ). Create the new node and allocate memory for that node. Read the data in new nodes data part. Assign cur->link =prev->link Assing prev=cur.
Step 4: If the operation type is deletion then i) If deletion at beginning is true then 1. Assign first=last->link->link (Move the first to second position) 2. Assign cur=last->link 3. Assign last->link=first 4. Reallocate the cur from memory. ii) If deletion between any two nodes is true 1. 2. 3. 4. Move the cur to required position. Move the prev to cur predecessors position. Assign prev->link=cur->link Reallocate cur from memory.
iii). If deletion at ends is true 1. 2. 3. 4. Move the cur to last position. Move the prev to curs predecessor position Assign prev->link=cur->link Reallocate cur from memory
Step 5: If operation type is traverses 1. Assign cur=first 2. Repeat the process untill cur becomes last Cur=cur->link
PROGRAM: //Circular Linked List in CPP #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define null 0 struct node { int info; struct node *link; public: }*start,*last; void main() { int ch,n,m,position,i; void create(int); void addat(int); void addbt(int,int); void del(int); void disp(); last=null; clrscr(); while(1)
{ cout<<"\n\nMAIN MENU"; cout<<"\n\n1.create\n2.addat\n3.addbt\n4.del\n5.disp\n6.exit"; cout<<"\n\nENTER YOUR CHOICE : "; cin>>ch; switch(ch) { case 1: cout<<"\n\nENETER NO OF ITC : "; cin>>n; for(i=0;i<n;i++) { cout<<"\n\nENTER THE ELEMENT : "; cin>>m; create(m); }break; case 2: cout<<"\n\nENTER THE ELEMENT : "; cin>>m; addat(m); break; case 3: cout<<"\n\nENTER THE ELEMENT : "; cin>>m; cout<<"\n\nENTER THE POSITION : ";
cin>>position; addbt(m,position); break; case 4: if(last==null) { cout<<"\n\nLIST IS EMPTY"; continue; } cout<<"\n\nENTER THE ELEMENT FOR DELETE : "; cin>>m; del(m); break; case 5: disp(); break; case 6: exit(0); break; default: cout<<endl<<endl<<"wrong choice"; } } } void create(int data)
{ struct node *q,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->info=data; tmp->link=null; if(last==null) { last=tmp; tmp->link=last; } else { tmp->link=last->link; last->link=tmp; last=tmp; } return; }
tmp->info=data; tmp->link=last->link; last->link=tmp; } void addbt(int data,int pos) { struct node *tmp,*q; int i; q=last->link;; for(i=0;i<pos-1;i++) { q=q->link; if(q==last->link) { cout<<"\n\nthere r lessthan "<<pos<<"elements "; return; } } tmp=(struct node *)malloc(sizeof(struct node)); tmp->link=q->link; tmp->info=data; q->link=tmp; if(q==last) last=tmp; }
void del(int data) { struct node *tmp,*q; if(last->link==last&&last->info==data) { tmp=last; last=null; free(tmp); cout<<"\n\nElement is "<<data<" deleted "; return; } q=last->link; if(q->info==data) { tmp=q; last->link=q->link; free(tmp); cout<<"\n\nElement is "<<data<"deleted "; return; } while(q->link!=last) { if(q->link->info==data) { tmp=q->link;
q->link=tmp->link; free(tmp); cout<<"\n\nElement is "<<data<<" deleted "; return; } q=q->link; } if(q->link==last) { if(q->link->info==data) { tmp=last; last=q; last->link=tmp->link; free(tmp); cout<<"\n\nElement is "<<data<<" deleted "; return; } }
void disp()
{ struct node *q; if(last==null) { cout<<"\n\nlist isdempty"; return; }q=last->link; cout<<endl; while(q!=last) { cout<<q->info<<" "; q=q->link; } cout<<last->info; return; }
Sample Input and output CIRCULAR LINKED LIST --------------------------------MIAN NENU 1.CREATE 2.INSERT 3.DELETE 4.EXIT Enter your choice:1 Type -999 to stop Enter the data:10 20
30 -999 Circular list 10 20 30 MIAN NENU 1.CREATE 2.INSERT 3.DELETE 4.EXIT Enter your choice:2 Enter the new item:40 Position of insertion:2 Circular list 10 40 20 30 MIAN NENU 1.CREATE 2.INSERT 3.DELETE 4.EXIT Enter your choice:3 Data to be deleted:20 Circular List 10 40 30 MIAN NENU 1.CREATE 2.INSERT 3.DELETE 4.EXIT Enter your choice:3 Data to be deleted:60 Element not found