LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.

Slides:



Advertisements
Similar presentations
Slide 1 Insert your own content. Slide 2 Insert your own content.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
0 - 0.
Chapter 17 Linked Lists.
1 Traversing a List Iteration. Idiom for traversing a null-terminated linked list. for (Node x = first; x != null; x = x.next) { StdOut.println(x.item);
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
EENG212 Algorithms and Data Structures
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
DATA STRUCTURES USING C++ Chapter 5
Chapter 4 Lists Pointers Singly Linked Lists
Linked Lists Linked Lists Representation Traversing a Linked List
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Using C++
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
CSCE 3110 Data Structures & Algorithm Analysis
John Hurley Cal State LA
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Addition 1’s to 20.
Test B, 100 Subtraction Facts
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Doubly-linked list library.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND.
WELCOME TO Linked List, Stack & Queue By Sumit Kumar PGT(CS) KV, Samba.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND.
Reference: Vinu V Das, Principles of Data Structures using C and C++
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Algorithms and Data Structures
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
UNIT-II Topics to be covered Singly linked list Circular linked list
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field.
Programming Circular Linked List.
Lectures linked lists Chapter 6 of textbook
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Traversing a Linked List
Linked Lists.
DATA STRUCTURE QUEUE.
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
LINKED LIST.
Review & Lab assignments
KENDRIYA VIDYALAYA SANGATHAN (AGRA REGION)
Presentation transcript:

LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley

LINKED LIST Singly Linked List: Contains the address of the next node. Doubly Linked List: Contains the address of the next node as well as address of previous nodes also. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

OPERATIONS TO BE PERFORMED IN LINKED LIST Insertion of Node In the beginning At the End of the Linked List Deletion of the Node Traversal of the Linked List SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

INSERTION OF THE NODE ( IN THE BEGINNING) Algorithm 1.Ptr = start 2.newptr = new node 3.If newptr = NULL 4.Print No Space Available (Overflow)! Exiting 5.Else 6.{ newptr -> data = item 7.Newptr -> next = NULL SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

8. if start = NULL then 9. start = newptr 10. else { 11. save = start 12. start = newptr 13. newptr -> next = save 14. } } 15. End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

IMPLEMENTATION OF THE ALGORITHM IN PROGRAM #include struct node { int info; node *next; } *start,*newptr,*ptr,*save; SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE node* create_new_node( int n ) { ptr=new node; 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; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE void display(node* np) { while (np!=NULL) { cout info "; np=np->next ; } cout >>>>!!!!!!"; }

void main() { start= NULL; int inf ; char ch='y'; while(ch=='y' || ch=='Y') { clrscr(); cout<<"\n enter information of the new node.."; cin>>inf ; cout<<"\n creating new node ! ! press enter to continue..."; getch(); SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE newptr = creat_new_node(inf); if( newptr!= NULL) { cout<<"\n \n new code created successfully. press enter to cointinue.."; getch(); } else { cout<<"\n cannot create new code \t "; exit(1); }

cout<<"\n \t now inserting this node in the beginning of the list "; cout<<"\n \t press enter continue "; getch(); insert_beg(newptr); cout<<"\n now the list is: \n"; display(start); cout<<"\n \a \t press enter y to enter new node, n to exit: " ; cin>>ch; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

INSERTION OF THE NODE ( AT THE END) Algorithm 1.Ptr = start 2.newptr = new node 3.If newptr = NULL 4.Print No Space Available (Overflow)! Exiting 5.Else 6.{ newptr -> data = item 7.newptr -> next = NULL SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

8. if start = NULL then { 9. start = newptr 10. rear = newptr } 8. else { 9. rear -> next = newptr 10. rear = newptr } 8. End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

IMPLEMENTATION OF THE ALGORITHM IN PROGRAM #include struct node { int info; node *next; } *start,*newptr,*ptr,*save, *rear; SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE node* creat_new_node( int n ) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; }

void insert_End (node* np) { if(start==NULL) { start=np; rear = np; } else { rear -> next = np; rear = np; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE void display(node* np) { while (np!=NULL) { cout info "; np=np->next ; } cout >>>>!!!!!!"; }

void main() { start= rear = NULL; int inf ; char ch='y'; while(ch=='y' || ch=='Y') { clrscr(); cout<<"\n enter information of the new node.."; cin>>inf ; cout<<"\n creating new node ! ! press enter to continue..."; getch(); SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE newptr = creat_new_node(inf); if( newptr!= NULL) { cout<<"\n \n new code created successfully. press enter to continue.."; getch(); } else { cout<<"\n cannot create new code \t "; exit(1); }

cout<<"\n \t now inserting this node at the end of the list "; cout<<"\n \t press enter continue "; getch(); insert_end(newptr); cout<<"\n now the list is: \n"; display(start); cout<<"\n \a \t press enter y to enter new node, n to exit: " ; cin>>ch; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

DELETION OF THE NODE FROM THE BEGINNING Algorithm 1.If start = NULL then 2.Print Underflow 3.Else { 4.Ptr = start 5.Start = start ->next 6.Delete ptr } 7.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

IMPLEMENTATION OF THE ALGORITHM IN PROGRAM void DelNode( ) { if (start == NULL) cout << \n Underflow!!!!!; else { ptr = start; start = start ->next; delete ptr; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

STACK A LIFO Structure Can be implemented as an array or as a linked list. Operations in Stack: Push (Addition of Elements) Pop (Removal / Deletion on elements) SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

STACK AS AN ARRAY (POP) Algorithm 1.If top < -1 then 2.print underflow !!!!!!! Aborting 3.Else { 4.Data = stack [top] 5.Top = top -1 } 6.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

STACK AS AN ARRAY (PUSH) Algorithm 1.Top = Read item 3.If top == (size – 1) then 4.Print No Space Available (Overflow)! Exiting 5.Else 6.{ top = top Stack[top] = item } 8.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

#include const int sz = 50; void push(int stack[], int &top, int ele) { if(top == (sz - 1)) { cout << "\n Stack overflow"; exit(1); } else { top ++; stack[top = ele; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

void pop(int stack[], int &top){ if(top < 0) { cout << "\n Underflow"; exit(1); } else cout << "\n Poped Element: " << stack[top--]; } void Display(int stack[], int top) { if(top == -1) return; for( int i = top; i >= 0; i--) cout << stack[i] <<endl; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

void main(){ int stack[sz], item, top = -1, res; char ch = 'y'; clrscr(); while(ch == 'y' || ch == 'Y'){ cout << "\n Enter item for insertion: "; cin >> item; push(stack, top, item); cout << "\n Do you want to enter more elements (y/n) : "; cin >> ch; } cout << "\n The stack now is: "; Display(stack, top); SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

cout << "\n Do you want to delete elements (y/n) : "; cin >> ch; while(ch == 'y' || ch == 'Y'){ pop(stack,top); cout << "\n Do you want to enter more elements (y/n) : "; cin >> ch; } cout << "\n The stack now is: "; Display(stack, top); } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

STACK AS A LINKED LIST (PUSH) Algorithm 1.Newptr = new Node 2.Newptr -> info = item; Newptr -> next = NULL 3.If top == NULL ) then 4.top = Newptr 5.Else 6.{ Newptr -> next = top 7.top = Newptr } 8.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

STACK AS A LINKED LIST (POP) Algorithm 1.If top == NULL then 2.Print Stack is Empty (Underflow), Exit 3.Else { 4.Print Top -> info 5.Top = top -> next } 6.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

IMPLEMENTATION OF THE ALGORITHM IN PROGRAM #include struct node { int info; node *next; } *top,*newptr,*ptr,*save; SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE node* creat_new_node( int n ) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; }

void Push (node* np) { if(top==NULL) top = np; else { save = top; top = np; np-> next = save; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE void display(node* np) { while (np != NULL) { cout info "; np = np->next ; } cout >>>>!!!!!!"; }

void Pop( ) { if (top == NULL) { cout << \n Underflow!!!!!; exit(1); } else { ptr = top; top = top ->next; delete ptr; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

void main() { top = NULL; int inf ; char ch='y'; while(ch=='y' || ch=='Y') { clrscr(); cout<<"\n enter information of the new node.."; cin >> inf ; cout<<"\n creating new node ! ! press enter to continue..."; getch(); SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE newptr = creat_new_node(inf); if( newptr != NULL) { cout<<"\n \n new code created successfully. press enter to continue.."; getch(); } else { cout<<"\n cannot create new code \t "; exit(1); }

cout<<"\n \t now Pushing this node into the stack "; cout<<"\n \t press enter continue "; getch(); Push(newptr); cout<<"\n now the Stack is: \n"; display(top); cout<<"\n \a \t press y to enter new node, n to exit: " ; cin>>ch; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

APPLICATIONS OF STACK Reversing a String Polish String Conversion of Infix Expression to Postfix Expression Evaluation of Postfix Expression Processing of Function (Subprogram) Calls Matching Parenthesis SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

Infix Notation Operators placed between two operands. E.g. A + B, A – B, A * B, A / B, A ^ B etc Postfix Notation (Reverse Polish Notation) Operators placed after two operands. E.g. AB +, AB -, AB * etc. Prefix Notation (Polish Notation) Operators placed before two operands. E.g. + AB, - AB, * AB etc. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE The Name Polish Notation and Reverse Polish Notation are named after Polish Logician Jan Lukasiewiez

CONVERSION FROM INFIX TO POST FIX (ALGORITHM) 1.Enclose the exp in Parenthesis i.e. ( ) 2.Read Next symbol of the exp and repeat steps 3 to 6 until the stack is empty and go to step 7 3.If symbol read = operand then add it to Postfix Exp. 4.If symbol read = ( then push it into the stack 5.If the symbol read = operator then 1.Repeat while ( Priority of Top(stack) >= Priority of operator ) 1.{pop operator from stack 2. Add Operator to Postfix Expression } 2.Push Operator into Stack 6.if the Symbol read = ) then 1.Repeat while ( Top(stack) != ( ) 1.{pop operator from stack 2. Add Operator to Postfix Expression } 2.Remove the (, It must not be added to Postfix Expression 7.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

EVALUATION OF POSTFIX EXPRESSION: 1.START 2.Read the element 3.If element = operand then Push the element into stack 4.If element = operator then Pop two operands from stack (One operand in case of NOT Operator) Evaluate the expression formed by two operands and the operator. Push the result of expression into stack 5.If no more elements then Pop the result 6.Else Goto step 2 7.END SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

QUEUE SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

THE QUEUE OPERATIONS A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE $ Front Rear

THE QUEUE OPERATIONS New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE $ Front Rear

THE QUEUE OPERATIONS When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE $ Front Rear

ARRAY IMPLEMENTATION A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... An array of integers to implement a queue of integers 486 We don't care what's in this part of the array.

ARRAY IMPLEMENTATION The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2

A DEQUEUE OPERATION When an element leaves the queue, size is decremented, and first changes, too. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 2 first 1 last 2

AN ENQUEUE OPERATION When an element enters the queue, size is incremented, and last changes, too. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 1 last 3

AT THE END OF THE ARRAY There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 3 first 3 last 5

AT THE END OF THE ARRAY The new element goes at the front of the array (if that spot isnt already used): SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 4 first 3 last 0 4

ARRAY IMPLEMENTATION Easy to implement But it has a limited capacity with a fixed array Or you must use a dynamic array for an unbounded capacity Special behavior is needed when the rear reaches the end of the array. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2

INSERTION IN THE QUEUE : Algorithm 1.Start 2.If rear = null (-1) then { 3.Rear = Front = 0 4.Queue [0] = item } 5.Else if Rear = N – 1 then 6. Print OVERFLOW 7.Else 8. Queue [Rear ++ ] = item 9.END SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

DELETION IN THE QUEUE: Algorithm Start If Front = null (-1) then Print UNDERFLOW Else { Item = Queue [Front] If Front = Rear Then Front = Rear = 0 } Else Front ++ END SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

#include int insert_in_Q(int[],int); void display(int[],int,int); const int size = 50; int Q[size], front=-1, rear=-1; SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Implementation of the Algorithm in Program: int insert_in_Q(int Q[ ], int ele) { if (rear==size-1) return -1; else if (rear==-1) { front = rear = 0 ; Q[rear]=ele; } else { rear++; Q[rear]=ele; } return 0; }

int remove(int Q[ ]) { int ret; if(front == -1) return -1 ; else { ret = Q[front]; if(front==rear) front=rear=-1; else front++; } return ret; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Implementation of the Algorithm in Program: void display (int Q[ ],int front,int rear) { if( front == -1) return; for(int i=front; i<=rear; i++) cout<<Q[i] << '\t'; }

void main() { clrscr(); int item, res ; char ch='y' ; while(ch=='y' || ch=='Y') { cout<<"\n\t enter element to be inserted : "; cin>> item; res = insert_in_Q(Q,item); // res= remove(Q) if( res==-1) { cout<<"n!!! sorry OVERFLOW !!! aborting## "; exit(1); } cout<<"\n \a\tnow the queue is : "; display (Q,front,rear); cout<<"\n\t\twant to insert more elements (y/n) :"; cin>>ch; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Implementation of the Algorithm in Program: cout << "\n DO you want to remove items from the queue: "; cin >> ch; while(ch == 'y' || ch == 'Y') { int de = remove(Q); if (de == -1) cout << "\n Queue is empty, !!!!! UNDERFLOW !!!!!!"; else { cout << "\n Deleted item is : " << de; cout<<"\n \a\tnow the queue is : "; display (Q,front,rear); cout << "\n Do you want to remove more items from the queue: "; cin >> ch; } }}

LINKED LIST IMPLEMENTATION A queue can also be implemented with a linked list with both a head and a tail pointer. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE null 13 head_ptr tail_ptr

LINKED LIST IMPLEMENTATION Which end do you think is the front of the queue? Why? SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE null 13 head_ptr tail_ptr

LINKED LIST IMPLEMENTATION The head_ptr points to the front of the list. Because it is harder to remove items from the tail of the list. SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE null head_ptr 13 tail_ptr Front Rear

INSERTION IN THE QUEUE: Algorithm 1.Newptr = new Node 2.Newptr -> info = item; Newptr -> next = NULL 3.If Rear == NULL) then { 4.Front = Newptr 5.Rear = Newptr } 6.Else { 7.Rear -> next = Newptr 8.Rear = Newptr } 9.End SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

DELETION IN THE QUEUE: SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Algorithm 1. If Front = NULL then 2. Print UNDERFLOW 3. Else { 4. Ptr = Front 5. If Front = Rear Then 6. Front = Rear = NULL 7. Else Front = Front - > next } 8. Delete ptr 9. End

#include struct node { int info; node *next; } *front,*newptr,*ptr,*save,*rear ; int itemdel; node* creat_new_node( int n) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; } SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Implementation of the Algorithm in Program: void insert_end (node* np) { if(front==NULL) front= rear=np; else { rear->next= np; rear=np; } } void display(node* np) { while (np != NULL) { cout info "; np = np->next ; } cout "; }

void delnode_q( ) { if (front==NULL) cout "; else { ptr = front; itemdel = ptr->info; front = front ->next; delete ptr; } void main() { front = rear = NULL; int inf ; char ch='y'; SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Implementation of the Algorithm in Program: while(ch=='y' || ch=='Y') { clrscr(); cout<<"\n enter information of the new node.."; cin >> inf ; cout<<"\n creating new node ! ! press enter to continue..."; getch(); newptr = creat_new_node(inf); if( newptr == NULL) { cout<<"\n \n CANNOT CREATE NODE"; exit(1); getch(); } insert_end(newptr );

cout<<"\n now the queue is :"; display(front); cout<<"\n \a \t press y toenter new node, n to exit: " ; cin>>ch; } cout<<"\n Do you want to delete element (y/n): " ; cin >> ch; while (ch == 'y' || ch == 'Y') { delnode_q(); SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Implementation of the Algorithm in Program: cout << "\n The deleted item is: " << itemdel; cout<<"\n now the Queue is: \n"; display(front); cout<<"\n Do you want to delete another element (y/n): " ; cin >> ch; } cout<<"\n now the Queue is: \n"; display(front); getch(); }

SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE

SARAS M SRIVASTAVA, PGT (CS), KV, IISC, BANGALORE Questions