Stack and Queues Stack implementation using Array

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.
Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND.
 Balancing Symbols 3. Applications
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Algorithms and Data Structures
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
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.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
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.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
STACKS & QUEUES for CLASS XII ( C++).
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Data Structure By Amee Trivedi.
Linked List Insert as a first node Insert as a last node
Data Structure #1: The Array
QUEUES.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
MEMORY REPRESENTATION OF STACKS
Algorithms and Data Structures
QUEUE.
Stacks Stack: restricted variant of list
Stack.
Pointers and Linked Lists
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Mark Allen Weiss, Addison Wesley
Popping Items Off a Stack Lesson xx
ليست هاي پيوندي.
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
UNIT-I Topics to be covere d 1.Introduction to data structures.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Review & Lab assignments
Queues FIFO Enqueue Dequeue Peek.
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
Stacks and Queues.
Stacks CS-240 Dick Steflik.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Linked Lists.
Presentation transcript:

Stack and Queues Stack implementation using Array Stack implementation using Linked List Queue implementation using Array Queue implementation using Linked List Convert Infix Exp into Postfix Evaluate Postfix Expression Convert Postfix into Infix

Queue Implementation Using Linked List Queue is a data structure in which front pointer point to front element of queue and rear pointer point to rear element of queue . It is implemented with the help of pointers and self referential structure. Each element ( also called node ) contain two fields : Information part and Pointer. Front struct Student { int RollNo; char Name[20] ; Student * link ; } InfoPart Link InfoPart Link Rear InfoPart Link Null

student *Front = NULL, *Rear = NULL; void main ( ) int c ; do struct Student { int RollNo; char Name[20] ; Student * link ; } ; student *Front = NULL, *Rear = NULL; void main ( ) int c ; do clrscr( ); cout << “1.Insert\n2.Delete\n3.Show\nExit Any other number\n”; cin>>c ; switch ( c ) case 1 : Insert ( ) ; break ; case 2 : Delete ( ) ; break ; case 3 : show ( ) ; break ; } } while(c>=1 && c<= 3);

Front void Insert ( ) { //create node Student *ptr ; ptr = new Student ; if ( ptr == NULL ) // checking overflow { cout << “Overflow “ ; return ; } //assigning Values cout << “Enter RollNo “ ; cin >> ptr -> RollNo; cout << “Enter Name “ ; gets( ptr -> RollNo ) ; ptr - > link =NULL; // Insert Node (Element) if ( Front = = NULL ) { Front=Rear = ptr ; } else Rear- > link =Ptr ; Rear= ptr ; } Rear Null Null InfoPart Link Ptr Null Front InfoPart Link InfoPart Link Rear Null InfoPart Link Null Ptr

Front void Delete ( ) { student *ptr ; if ( Front = = NULL ) { cout<<“Underflow “ ; return; } ptr=front; if (front==rear) front=rear=NULL; } else front=front - > link ; cout<<ptr->name<<“is deleted”; delete ptr ; Null Front InfoPart Link InfoPart Link InfoPart Link Null Rear Ptr

void Display ( ) { student void Display ( ) { student *ptr ; if ( Front = = NULL ) { cout<<“Underflow “ ; } else ptr=front; while (ptr!=NULL) cout<<“Name”<<ptr->Name<<“,”<<“RollNo”<<ptr->RollNo; ptr=ptr - > link ; }

Queue using Array Insert 10 Insert 20 Insert 30 Delete Insert 50 4 3 Front 30 2 Rear Front 20 1 Rear Front 10 Rear Front = -1,Rear=-1 50

const int max=100; void main ( ) { int DATA [max ] ,Front= -1,Rear=-1, c ; do clrscr( ); cout << “1.Insert\n2.Delete\n3.Show\nExit Any other number\n”; cin>>c ; switch ( c ) case 1 : Insert ( DATA , Front, Rear) ; break ; case 2 : Delete ( DATA , Front,Rear) ; break ; case 3 : show ( DATA , Front,Rear) ; break ; } } while(c>=1 && c<= 3);

void Insert ( int Num [ ] , int & Front , int & Rear) { int item ; //Check overflow condition if ( Rear == max – 1 ) cout<<“Overflow “; return; } if (Rear==-1) front=rear=0; else Rear =Rear +1; cout << “Enter Item To Push into the stack\n; cin >> item; Num [ Rear ] =item; cout<<“Item Inserted “;

void Delete ( int Num [ ] , int &Rear, int &Front ) { //Check underflow condition if ( Front == – 1 ) cout<<“underflow “; return; } if (Front==Rear) cout << Num[ Front ]<<“is to be deleted \n“; Front= Rear =-1; else Front=Front+1;

void show ( int Num [ ] , int Front ,int Rear ) { //Check underflow condition if ( Front== – 1 ) cout<<“underflow “; } else for ( i= Front; i <=Rear ; i ++ ) cout<< Num [ i ] << endl ;