Presentation is loading. Please wait.

Presentation is loading. Please wait.

WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND.

Similar presentations


Presentation on theme: "WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND."— Presentation transcript:

1 WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND

2 Linked-List A link-list is a linear collection of data elements, called nodes pointing to the next nodes by means of pointers. A linked list consists of a series of nodes which are not necessarily adjacent in memory. Link Data Elements

3 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.

4 Memory Allocation (Dynamics Vs Static) Each data element, stored in the memory,is given some memory. this process of giving memory is called memory allocation. Static memory: This memory allocation technique reserves fixed amount of memory before actual processing takes place, therefore the number of elements to be stored must be predetermined. Example: Array Dynamic Memory Allocation: An allocation of memory during the program execution itself, as and when required.Example: Link List

5 Array vs. Linked List An array is a static data structure. It cannot be created nor destroyed during program execution. So there is a lot of memory wastage. Size of an array should be known in advance. In array, insertion and deletion require lot of shifting. All these problems are eliminated in linked list

6 4647484950 3637383940 2627282930 1617181920 67891041424344453132333435 2122232425 1112131415 12345 (45)(32) (34)(39)(20) (11) (6)(25) (9) Example of a Linked List

7 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F00100011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F Memory Linked list inside the Computer Memory

8 00000001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F Memory00100011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F 0002 Linked list inside the Computer Memory

9 00000001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F Memory00100011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F 0007 0002 Linked list inside the Computer Memory

10 00000001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F Memory00100011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F 0007 0018 0002 Linked list inside the Computer Memory

11 Singly, Doubly and Circular Linked List

12 Free Store Allocation in c++ In C++,Every program is provided with a pool of unallocated memory that it may utilize during execution. this memory is known as free store memory. Free store memory is allocated by applying operator new to a type specifier and which returns a pointer to the allocated memory.

13 Free Store Allocation in c++ struct Node {char info; Node *next; }; Node *ptr; ptr = new Node; To Refered to info part,we may write ptr->info; To Refered to next pointer,we may write ptr->next; When a node is deleted, it is done as delete ptr;

14 Basic Operations on singly linked list: New ITEM is either added in beginning of the list or in the middle of the list or in the end of the list. T o add an ITEM in the beginning of the list, START is modified to point to the new node of ITEM and the nest pointer of the new node(i.e, ITEM node) points to the previous first node. T o add an ITEM in the end of the list, nest pointer of the last node is made to point to the new ITEM’s node and the next pointer of the new ITEM/s node is made NULL. IF you try to insert a node,when there is no memory available,it is called “OVERFLOW”

15 Insertion Beginning MID END

16 Insertion in Link List #include 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); }

17 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 info ”; np=np->next; } cout<<“!!!\n”; } void insert_end(Node *np) { if( start= =NULL) start=rear=np; else { rear->next=np; rear=np;} }

18 Deletion in Link List #include 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( ); }

19 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 info ”; np=np->next; } cout<<“!!!\n”; } void delnode( ) { if( start= =NULL) cout<<“Underflow”; else { ptr=start; start=start->next; delete ptr;} }

20 Traversal in Link List #include 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); }

21 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 info ”; np=np->next; } cout<<“!!!\n”; } void traverse(Node *np) { while(np!= NULL) { cout info ”; np=np->next; } cout<<“!!!\n”; }

22 Stack 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.

23 Stack Operation

24 Queues 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.

25 Queues

26 Application of Stack :Polish Strings PUSHA B ADD PUSHC D ADD MUL POPX

27


Download ppt "WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND."

Similar presentations


Ads by Google