STACKS AND QUEUES. A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO)

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
CSCI2100B Linked List Jeffrey
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Data Structure Dr. Mohamed Khafagy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Linked List Spring 2013Programming and Data Structure1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CSC2100B Tutorial 2 List and stack implementation.
STRUCTURES (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Introduction Queue is a linear Data Structure in which the operations are performed based on FIFO (First In First Out) principle.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
CE 221 Data Structures and Algorithms
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
 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.
STACKS & QUEUES II Array Based Approach.
Review Array Array Elements Accessing array elements
UNIT II Queue.
Lists, Stacks and Queues in C
Abstract Data Types.
Queues Rem Collier Room A1.02
Queue data structure.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
Stack and Queue.
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.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
CE 221 Data Structures and Algorithms
CSI 1340 Introduction to Computer Science II
Queue.
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
Structures and List Processing
Presentation transcript:

STACKS AND QUEUES

A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO) data structure

The header file: queue.h #define EMPTY 0 #define FULL typedef int data; typedef enum {false, true} boolean; struct node { int d; struct node *next;}; typedef struct node node; struct queue { int cnt; /* count of the elements */ node *front; /* ptr to the front element */ node *rear; /* ptr to the rear element */ }; typedef struct queue queue;

The header file: queue.h data front(const queue *q); boolean isempty(const queue *q); boolean isfull(const queue *q); void initialize(queue *q); void enqueue(data x, queue *q); data dequeue(queue *q);

Basic queue routines : ISEMPTY(),ISFULL() boolean isempty(const queue *q) /* RETURNS TRUE IF QUEUE IS EMPTY */ { return ((boolean) (q -> cnt == EMPTY)); /* if(q->cnt==EMPTY) return true; else return false*/ } boolean isfull(const queue *q)/* RETURNS TRUE IF QUEUE IS FULL */ {return ((boolean) (q -> cnt == FULL));}

Basic queue routines : FRONT(), INITIALIZE() data front(queue *q) // RETURNS DATA OF FRONT NODE { return (q -> front -> d);} void initialize(queue *q) // Count=0, No front and rear nodes {q -> cnt = 0; q -> front = NULL; q -> rear = NULL;}

Basic queue routines : ENQUEUE() /* Creating queue  enqueue */ void enqueue(data x, queue *q) {node *p; p = malloc(sizeof(node)); // CREATE A NEW NODE p -> d = x; p -> next = NULL; if (!isempty(q)) /* IF QUEUE IS NOT EMPTY APPEND NEW NODE FROM REAR */ { q -> rear -> next = p; q -> rear = p;} else/* IF QUEUE IS EMPTY NEW NODE WILL BE FRONT AND REAR */ q -> front = q -> rear = p; q -> cnt++;}

data dequeue(queue *q) { data x; node *p; x = q -> front -> d; /* store data in x */ p = q -> front; /* store address of front node that will be deleted */ q -> front = q -> front -> next; /* determine new front node */ q -> cnt- - ; /* decrease count */ free(p); /* free node */ return x;} Basic queue routines : DEQUEUE()

void print(queue *q) { node * p; for(p=q->front; p!=NULL; p=p->next) printf("%d ", p->d); printf(“\n”); } Basic queue routines :PRINT()

main() { queue my_q;data x;int op; initialize(&my_q); do {printf(" PLEASE SELECT OPERATION\n"); printf(" ENQUEUE 1\n DEQUEUE 2\n FRONT 3\n IS FULL 4\n IS EMPTY 5\n PRINT 6 \n EXIT 7\n" ); scanf("%d",&op); /* SWITCH- CASE  NEXT SLIDE */ } while(op!=7); }

switch(op) {case 1 :printf("Enter data : "); scanf("%d",&x); enqueue(x,&my_q); break; case 2:dequeue(&my_q); break; case 3:printf(" front: %d",front(&my_q)); break; case 4:if(isfull(&my_q)==true) printf("Queue is FULL"); else printf("Queue is NOT FULL"); break; case 5:/* similar to case 6 */ case 6:print(&my_q); break; case 7:exit(0); default :break;}

Array Implementation of Queues An array to store elements, Array, and the positions Front and Rear to represent the ends of the queue are kept. We also keep track of the number of elements: Size. 12

13 Array Implementation of Queues – Problem 5271 FrontRear To enqueue an element X, increment Size and Rear and set Array[Rear]=X To dequeue an element, set the return value to Array[Front], decrement Size and then increment Front. Assume after several enqueue operations, the Rear is at the last index position. Assume also that some elements, in the meantime, have been dequeued to make up room. The next enqueue would fail, although there would be free slots.

The simple solution is whenever Front or Rear gets to the end of the Array, it is wrapped around to the beginning (hence the name circular array). 14 Array Implementation of Queues – Problem Solved 24 FrontRear 124 FrontRear 1324 FrontRear initially After enqueue(1) After enqueue(3)

QUEUE : Array Implementation #define MAX_QUEUE_SIZE 10 typedef struct { int cnt; int front; int rear; data[MAX_QUEUE_SIZE]; } queue;

QUEUES(array imp. ) : Basic routines void initializeQueue(queue * q) { q -> cnt = 0; q -> front = 0; q -> rear = -1; } boolean IsEmpty( queue * q ) { return q->cnt == 0; } boolean IsFull( queue * q ) { return q->cnt == MAX_QUEUE_SIZE; }

QUEUES(array imp. ) : Basic routines void enqueue(queue * q, int x){ if (!IsFull(q)){ q->rear++; q->cnt++; if (q->rear == MAX_QUEUE_SIZE) q->rear = 0; q->data [q->rear] = x; }

QUEUES(array imp. ) : Basic routines int dequeue(queue * q){ if (!IsEmpty(q)){ int x=q->data[q->front]; q->cnt--; q->front++; if (q->front == MAX_QUEUE_SIZE) q->front= 0; return x; }

STACKS SAMPLE PSEUDO CODE: push(1) to Stack push(2) to Stack push(3) to Stack pop() from Stack push(4) to Stack

STACKS : Array Implementation #define STACK_SIZE 4 typedef struct { int data [STACK_SIZE]; int top; } stack;

STACKS(array imp. ) : Basic routines void reset(stack * stk) { stk->top=-1; } void push( int c, stack * stk) {if(stk->top!= STACK_SIZE-1) { stk->top++; stk->data[stk->top]=c; } elseprintf(“Stack is full!!\n”); } int pop (stack * stk) {if(stk->top!=-1)return (stk->data[stk->top- -]) else printf(“stack is empty”); }

STACKS (array implementation): main() x= pop(&n); push(11,&n); x= pop(&n); push(3,&n); push(2,&n); x= pop(&n);} main() { stack n; int x; reset(&n); push(4,&n); push(5,&n); push(3,&n); push(2,&n); push(1,&n);

Stacks: Linked List Implementation

#define EMPTY 0 #define FULL typedef char data; typedef enum {false, true} boolean; typedef struct { data d; struct node *next; } node ; typedef struct { int cnt; /* count of the elements */ node *top; /* pointer to the top element */ } stack; The header file: stack.h

boolean empty(const stack *stk); boolean full(const stack *stk); void initialize(stack *stk); void push(data d, stack *stk); char pop(stack *stk); char top(stack *stk); The header file: stack.h

boolean isempty(const stack *stk) { return (stk -> cnt == EMPTY); } boolean isfull(const stack *stk) { return (stk -> cnt == FULL); } void initialize(stack *stk) { stk -> cnt = 0; stk -> top = NULL; } data top(stack *stk) { return (stk -> top -> d); } Basic routines : Isempty(),Isfull(), Initialize(), Top()

void push(data d, stack *stk) { /* create new node */ node *p; p = malloc(sizeof(node)); p -> d = d; p -> next = stk -> top; /* assign new node as top node */ stk -> top = p; stk -> cnt++; } Basic routines : Push(),Pop() data pop(stack *stk) { data x; // will store the top data node *p; x = stk -> top -> d; p = stk -> top; stk -> top = stk -> top -> next; stk -> cnt- -; free(p); return x; }

void main(void) { char str[] = “CS115”; int i; stack s; initialize(&s); /* initialize the stack */ for (i = 0; str[i] != '\0'; ++i) /* fill stack */ { if (!full(&s)) push(str[i], &s); } printf(“String in the stack: "); while (!empty(&s)) putchar(pop(&s)); }