180 likes | 353 Views
WELCOME TO Linked List, Stack & Queue. By Sumit Kumar PGT(CS) KV , Samba. Linked-List. A link-list is a linear collection of data elements, called nodes pointing to the next nodes by means of pointers.
E N D
WELCOME TO Linked List, Stack & Queue By Sumit Kumar PGT(CS) KV , Samba
Linked-List A link-list is a linear collection of data elements, called nodes pointing to the next nodes by means of pointers. A stack is a linear structure implemented in LIFO manner where insertions and deletions are restricted to occur only at one end – stack’s top. The stack is also a dynamic data structure as it can grow or shrink. A Queue is also a linear dynamic structure implemented in FIFO manner where insertions can occur at the “rear” end, and deletions occur only at “front” end.
Important Point about Link list Linked list overcome the drawbacks of arrays as in linked list number of elements need not be predetermined, more memory can be allocated or released during the processing as and when required. Making insertions and deletions much easier and simpler.
struct Node { char info; Node *next; }; Node *ptr; ptr = new Node; ptr->info; ptr->next; delete ptr; Free Store Allocation in c++
Beginning Insertion MID END
#include<iostream.h> #include<process.h> struct Node { int info; Node *next; } *start, *newptr, *save, *ptr, *rear; Node * create_new_node(int); void insert_beg(Node *); void insert_end(Node *); void display(Node *); void main( ) {start = NULL; int inf; cout<<“Enter info for new node”; cin>>inf; cout<<“create new node !!”; newptr= create_new_node(inf); if(newptr!=NULL) cout<<“Success”; else { cout<<“Cannot create”; exit(1);} cout<<“insert new node in the beginning of list”; insert_beg(newptr); //Or insert_end(newptr); display(start); } Insertion in Link List
Node * create_new_node( int n) { ptr = new ptr; ptr->info=n; ptr->next=NULL; return ptr; } void insert_beg(Node *np) { if( start= =NULL) start=np; else { save=start; start=np; np->next=save;} } void display( Node *np) { while(np!=NULL) { cout<<np->info<<“->”; np=np->next; } cout<<“!!!\n”; } Insertion in Link List void insert_end(Node *np) { if( start= =NULL) start=rear=np; else { rear->next=np; rear=np;} }
#include<iostream.h> #include<process.h> struct Node { int info; Node *next; } *start, *newptr, *save, *ptr, *rear; Node * create_new_node(int); void insert(Node *); void display(Node *); void delnode(Node *); void main( ) {start = rear = NULL; int inf; cout<<“Enter info for new node”; cin>>inf; cout<<“create new node !!”; newptr= create_new_node(inf); if(newptr!=NULL) cout<<“Success”; else { cout<<“Cannot create”; exit(1);} cout<<“insert new node in the beginning of list”; insert(newptr); display(start); delnode( ); } Deletion in Link List
Node * create_new_node( int n) { ptr = new ptr; ptr->info=n; ptr->next=NULL; return ptr; } void insert(Node *np) { if( start= =NULL) start=rear=np; else { rear->next=np; rear=np;} } void display( Node *np) { while(np!=NULL) { cout<<np->info<<“->”; np=np->next; } cout<<“!!!\n”; } Deletion in Link List void delnode( ) { if( start= =NULL) cout<<“Underflow”; else { ptr=start; start=start->next; delete ptr;} }
#include<iostream.h> #include<process.h> struct Node { int info; Node *next; } *start, *newptr, *save, *ptr, *rear; Node * create_new_node(int); void insert(Node *); void traverse(Node *); void main( ) {start = rear = NULL; int inf; cout<<“Enter info for new node”; cin>>inf; cout<<“create new node !!”; newptr= create_new_node(inf); if(newptr!=NULL) cout<<“Success”; else { cout<<“Cannot create”; exit(1);} insert(newptr); traverse(start); } Traversal in Link List
Node * create_new_node( int n) { ptr = new ptr; ptr->info=n; ptr->next=NULL; return ptr; } void insert(Node *np) { if( start= =NULL) start=rear=np; else { rear->next=np; rear=np;} } void display( Node *np) { while(np!=NULL) { cout<<np->info<<“->”; np=np->next; } cout<<“!!!\n”; } Traversal in Link List void traverse(Node *np) { while(np!= NULL) { cout<<np->info <<“->”; np=np->next; } cout<<“!!!\n”; }
Stack refer to the lists stored and accessed in a special way i.e. LIFO technique. In stack, insertion and deletions take place only at one end called the top. Stack
Queues are FIFO lists, where insertions take place at the “rear” end of the queue and deletions take place at the “front” end of the queues. Queues