CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Similar documents
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)


Stack & Queue on Self-Referencing Structures

Stack & Queue on Self-Referencing Structures

Linked List. April 2, 2007 Programming and Data Structure 1

.:: UNIT 4 ::. STACK AND QUEUE

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

8. Fundamental Data Structures

UNIT-2 Stack & Queue

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

ITI Introduction to Computing II

Data Structure. Recitation VII

School of Electrical Engineering & Computer Science Oregon State University. CS Recitation 3 Spring 2012

Stacks and Queues. CSE 373 Data Structures Lecture 6

Lecture Data Structure Stack

CSC 1052 Algorithms & Data Structures II: Queues

Programming. Lists, Stacks, Queues

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Lecture 3: Stacks & Queues

Chapter 18: Stacks And Queues

1 P age DS & OOPS / UNIT II

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Ashish Gupta, Data JUET, Guna

Chapter 18: Stacks And Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Abstract Data Types. Definitions, Implementations, and Uses

CS24 Week 4 Lecture 2

Data Structure - Stack and Queue-

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Stack and Queue. Stack:

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Data Structures and Algorithms for Engineers

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Queue ADT. CMPT 125 Feb. 28

CSC 1052 Algorithms & Data Structures II: Linked Queues

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

UNIT VI. STACKS AND QUEUES

Your Topic Goes Here Queue

CSEN 301 Data Structures and Algorithms

Chapter 5. ADTs Stack and Queue

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

Queue with Array Implementation

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Postfix (and prefix) notation

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

BBM 201 DATA STRUCTURES

Queue: Queue Representation: Basic Operations:

LIFO : Last In First Out

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

csci 210: Data Structures Stacks and Queues

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Chapter 9 STACK, QUEUE

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

List, Stack and Queue Implementation

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS 261: Data Structures. Dynamic Array Queue

data structures and algorithms lecture 6

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

Stacks and Queues. CSE Data Structures April 12, 2002

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Type. structure. C Programming Lecture 11 : Structuret. struct union. typedef. A collection of one or more variables

STACKS AND QUEUES. Problem Solving with Computers-II

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

CE204 Data Structures and Algorithms Part 2

Queue Linked List Implementation

CMSC 132: Object-Oriented Programming II. Stack and Queue

DATA STRUCTURE UNIT I

CMSC Introduction to Algorithms Spring 2012 Lecture 7

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Queues. Queue ADT Queue Implementation Priority Queues

CSE 230 Intermediate Programming in C and C++

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Solution for Data Structure

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Sequence Abstract Data Type

Data Structures. Lecture 5 : The Queues

Transcription:

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) An interesting definition for stack element The stack element could be of any data type struct stackelement int etype; union int ival; float fval; char *sval; element; ; struct stack int top; struct stackelement items[100];

Printing the top element of the stack #define INGR 1 #define FLT 2 #define STRING 3 struct stack S; struct stackelement se; se=s.items[s.top]; switch(se.etype) case INGR: printf( %d\n,se.ival); case FLT: printf( %f\n,se.fval); case STRING: printf( %s\n,se.sval); Definition of a node of stack typedef struct _node char element; struct _node *next; node; typedef node *stack;

Initializing the stack stack init () stack S; /* Create the dummy node */ S = (node *)malloc(sizeof(node)); S -> element = '\0'; S -> next = NULL; return S; isempty and isfull functions int isempty ( stack S ) return (S -> next == NULL); int isfull ( stack S ) return 0;

The top of the stack char top ( stack S ) if (isempty(s)) fprintf(stderr, "top: Empty stack\n"); return '\0'; return S -> next -> element; The push function stack push ( stack S, char ch ) node *T; /* Copy the new element in the dummy node */ S -> element = ch; /* Create a new dummy node if (isfull(s)) */ fprintf(stderr, "push: Full T = (node stack\n"); *)malloc(sizeof(node)); return S; T -> element = '\0'; T -> next = S; return T;

The pop function stack pop ( stack S ) if (isempty(s)) fprintf(stderr, "pop: Empty stack\n"); return S; /* Treat the stack top as the new dummy node */ S -> next -> element = '\0'; return S -> next; Print the stack void print ( stack S ) node *T; T = S -> next; while (T!= NULL) printf("%c", T -> element); T = T -> next;

The queue ADT A queue is like a "natural" queue of elements. It is an ordered list in which all insertions occur at one end called the back or rear of the queue. All deletions occur at the other end called the front or head of the queue. In the popular terminology, insertion and deletion in a queue are respectively called the enqueue and the dequeue operations. The element dequeued from a queue is always the first to have been enqueued among the elements currently present in the queue. In view of this, a queue is often called a First-In-First-Out or a FIFO list. Initializing the queue queue init () queue Q; Q.front = 0; Q.back = MAXLEN - 1; return Q;

Operations on the queue ADT Q = init(); Initialize the queue Q to the empty queue. isempty(q); Returns "true" if and only if the queue Q is empty. isfull(q); Returns "true" if and only if the queue Q is full, provided that we impose a limit on the maximum size of the queue. front(q); Returns the element at the front of the queue Q or error if the queue is empty. Operations on the queue ADT Q = enqueue(q,ch); Inserts the element ch at the back of the queue Q. Insertion request in a full queue should lead to failure together with some appropriate error messages. Q = dequeue(q); Delete one element from the front of the queue Q. A dequeue attempt from an empty queue should lead to failure and appropriate error messages. print(q); Print the elements of the queue Q from front to back.

Implementations of the queue ADT We maintain two indices to represent the front and the back of the queue. During an enqueue operation, the back index is incremented and the new element is written in this location. For a dequeue operation, on the other hand, the front is simply advanced by one position. It then follows that the entire queue now moves down the array and the back index may hit the right end of the array, even when the size of the queue is smaller than the capacity of the array. Reducing wastage in the queue In order to avoid waste of space, we allow our queue to wrap at the end. This means that after the back pointer reaches the end of the array and needs to proceed further down the line, it comes back to the zeroth index, provided that there is space at the beginning of the array to accommodate new elements. Thus, the array is now treated as a circular one with index MAXLEN treated as 0, MAXLEN + 1 as 1, and so on. That is, index calculation is done modulo MAXLEN. We still don't have to maintain the total queue size. As soon as the back index attempts to collide with the front index modulo MAXLEN, the array is considered to be full.

Tackling Isempty and Isfull There is just one more problem to solve. A little thought reveals that under this wrap-around technology, there is no difference between a full queue and an empty queue with respect to arithmetic modulo MAXLEN. This problem can be tackled if we allow the queue to grow to a maximum size of MAXLEN - 1. This means we are going to lose one available space, but that loss is inconsequential. Now the condition for full array is that the front index is two locations ahead of the back modulo MAXLEN, whereas the empty array is characterized by that the front index is just one position ahead of the back again modulo MAXLEN. Isempty and Isfull int isempty ( queue Q ) return (Q.front == (Q.back + 1) % MAXLEN); int isfull ( queue Q ) return (Q.front == (Q.back + 2) % MAXLEN);

Enqueue queue enqueue ( queue Q, char ch ) if (isfull(q)) fprintf(stderr,"enqueue: Queue is full\n"); return Q; ++Q.back; if (Q.back == MAXLEN) Q.back = 0; Q.element[Q.back] = ch; return Q; Dequeue queue dequeue ( queue Q ) if (isempty(q)) fprintf(stderr,"dequeue: Queue is empty\n"); return Q; ++Q.front; if (Q.front == MAXLEN) Q.front = 0; return Q;