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 next 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 // The algorithm for insert in the beginning of the list is given below. 1. ptr= START // START denotes the first node of the list 2. NEWPTR=new node //allocate memory for new node 3. If NEWPTR=NULL // if sufficient memory is not available 4.Print “ no space available” 5. Else { NEWPTR  INFO=ITEM //put information in new node 6. NEWPTR  LINK=NULL // initialize the pointer 7.If START=NULL then 8. START=NEWPTR 9. else { Save=START // Save Start’s value-Save is also pointer 10. START=NEWPTR // Assign NEWPTR to START 11. NEWPTR  LINK=Save// Make NEWPTR point to previous START } 12. END

17 Insertion in Link List #include #include // for exit( ) 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); }

18 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;} }

19 //Insertion in the end of list 1.declare pointer START,PTR,NEWPTR,REAR 2.PTR=START 3. NEWPTR=new Node 4. If NEWPTR=NULL 7Print” No Space available”; 8Exit 9Else { 10. NEWPTR  LINK=NULL } 11. If START=NULL then 12 START=NEWPR 13 REAR=NEWPTR } 14. REAR  LINK=NEWPTR 16 REAR=NEWPTR 16END.

20 // Deletion from the Beginning of list //First of all initialize pointers 1.If START= NULL THEN 2. Print “ UNDERFLOW” Else { 3. ptr=START 4.START=ptr  LINK 5. Delete ptr } 7.END

21 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( ); }

22 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;} }

23 Traversal: Traversal of a linked list means processing all the nodes of the list one by one. Algorithms: //Initialize counter 1.ptr=start 2. repeat steps 3 and 4 until ptr=null 3. Print ptr  INFO 4. ptr=ptr  LINK 5. END

24 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); }

25 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”; }

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

27 Stack Operation

28 Algorithm: Pushing in stack Array Top=-1 Read Item If(top==N-1) Then { Print “Overflow: } Else { Top=top+1 Stack[top]=item } END

29 Algorithm: Popping from Array Stack(Remove) If top==-1 { print “ Underflow” Exit } Else { Print stack[top] } end

30 #include Int Push(int [ ],int &,int); Void display(int [ ],int); Const int size=50; Void main( ) { int Stack[size],Item,top=-1,res; Char ch=‘y’; Clrscr( ); While(ch==‘y’||ch=‘Y’) { cout<<“\n enter ITEM for insertion:”; Cin>>Item; res=Push(Stack,top,Item) if(res= = -1) { cout<<“\n Overflow”; exit(1); } cout<<“\n the stack now is:; Display(Stack,top); cout<<“ \n Want to insert more element?(y/n); cin>>ch; } int Push(int Stack[ ],int & top,int ele) { if(top= =size-1) return -1; else top++; Stack(top]=ele; } Return 0; } void Display( int Stack[ ],int top) { cout<< Stack[top]<<“  ”<<endl; For(int i =top-1;i>=0;i- -) cout<< Stack[i]<<endl; }

31 Conversion of infix to postfix expression: Evaluation order: 1. Brackets or Parenthesis 2.Exponentiation(^) 3. Multiplication or Division 4. Addition or Substraction Example: Convert (A+B)*C/D into postfix Ans: Step 1: Determine the actual evaluation order by putting braces. ((A+B)*C)/D Step 2: Converting expression into innermost braces. ((AB+)*C)/D=(AB+C*)/D=AB+C*D/

32 Conversion from infix to prefix An expression where an operator precede the two operands is a prefix expression INFIXPREFIX ((A+(B-C)*D)^E+F)T1=B-C-BC ((A+T1*D)^E+F)T2=T1*D*T1D ((A+T2)^E+F)T3=A+T2+AT2 (T3^E+F)T4=T3^E^T3E (T4+F)T5=T4+F+T4F T5+^+A*-BCDEF

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

34 QUEUE: A queue is a special type of data structure where elements are inserted from one end is known as rear end and elements are deleted from the other end is known as front end. The queue is also known as First in First out (FIFO). Front Store the index of first element in the queue and rear stores the index of last element in the queue. at beginning,front=0 rear=-1

35 Insertion in the array queue: Algorithm: 1.If rear=NULL then 2.{ 3. rear=front=0 4.Queue[0]=item 5.} 6.Else if rear=n-1 then 7.Print “queue is full, OVERFLOW” 8.Else {queue[rear+1]=item 9.Rear=rear+1 10.} 11.end

36 Deletion in a array queue: Algorithm: 1.If front=Null then 2. print “queue empty” 3.Else { 4.Item=queue[front] 5.If front=rear then 6.{ front=rear=null 7.} 8. else front =rear+1 9.} 10.end

37 Linked Queues: Algorithm: 1.//Allocate space for item to be inserted 2. NEWPTR=new Node 3.NEWPTR  INFO=item; 4.NEWPTR  LINK=NULL 5. if rear=NULL then 6. { front=NEWPTR 7. rear=NEWPTR 8. } else 9. { rear  LINK=NEWPTR 10. rear= NEWPTR 11. } 12. END

38 DELETION in a Linked Queue Algorithm: 1.If front=null then 2. print “ Queue empty” 3. else { 4. item= front  link 5.If front =rear then 6. { front=rear=null 7.} 8.Else 9.Front =front  link 10. } 11.end

39 Circular Queue: A Circular Array allows the entire array to store the element s without shifting any data within the queue. Doubled ended Queue: Deque are the refined queues in which elements can be added or removed at either end but not in the middle.

40

41


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

Similar presentations


Ads by Google