Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues.

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Stacks and Queues. 2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
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)
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
1 Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
Lists CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
Reference: Vinu V Das, Principles of Data Structures using C and C++
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
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.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
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.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
 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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
STACKS & QUEUES II Array Based Approach.
CS505 Data Structures and Algorithms
Stacks and Queues Chapter 4.
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
Linked Lists.
Stacks and Queues CMSC 202.
Basic Data Structures – Continued (Queues)
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Data Structures – Week #3
Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
Pointers and Linked Lists
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
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
Pointers & Dynamic Data Structures
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Data Structures – Week #3
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Infix to postfix conversion
Binary Tree Traversal.
Presentation transcript:

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues

2 Overview Linked Stack. –Push –Pop Linked Queue. –Append –Serve

3 Linked Stack Top of the Stack NULL pointer

4 #ifndef LINKEDSTACKH #define LINKEDSTACKH #include #include "node.h" struct LinkedStackRec { Node* topPtr; }; typedef struct LinkedStackRec Stack; void intializeStack(Stack* stackPtr); bool stackEmpty(const Stack* stackPtr); bool stackFull(const Stack* stackPtr); void push(Stack* stackPtr, float item); float pop(Stack* stackPtr); #endif

void initializeStack(Stack* stackPtr) { stackPtr->topPtr = NULL; } Initialize Stack 5

6 Push topPtr

7 Push create a new node for the item link the new node to the current top node make the new node the new top void push(Stack* stackPtr, float item) { Node* newNodePtr = makeNode(item); newNodePtr->nextPtr = stackPtr->topPtr; stackPtr->topPtr = newNodePtr; }

8 Pop topPtr

9 Pop check if the stack is empty remember the item in the top node remember address of current top node make the next node the new top free the old top node return the item

float pop(Stack* stackPtr) { float item; Node* oldNodePtr = stackPtr->topPtr; if (stackEmpty(stackPtr)) { fprintf(stderr, “Stack is empty\n”); exit(1); } else { item = oldNodePtr->value; stackPtr->topPtr = oldNodePtr->nextPtr; free(oldNodePtr); } return item; }Pop 10

11 Linked Queue frontPtrrearPtr

12 #ifndef LINKEDQUEUEH #define LINKEDQUEUEH #include #include "node.h" struct LinkedQueueRec { int count; Node* frontPtr; Node* rearPtr; }; typedef struct LinkedQueueRec Queue; void intializeQueue(Queue* queuePtr); bool queueEmpty(const Queue* queuePtr); bool queueFull(const Queue* queuePtr); void append(Queue* queuePtr, float item); float serve(Queue* queuePtr); #endif

void initializeQueue(Queue* queuePtr) { queuePtr->frontPtr = NULL; queuePtr->rearPtr = NULL; queuePtr->count = 0; } Initialize Queue 13

14 Append frontPtr rearPtr frontPtr rearPtr

15 Append create a new node for the item if the queue is empty: –the new node becomes both front and rear of the queue otherwise: – make a link from the current rear to the new node –the new node becomes the new rear increment the count

void append(Queue* queuePtr, float item) { Node* newNodePtr = makeNode(item); if (queueEmpty(queuePtr)) { queuePtr->frontPtr = newNodePtr; queuePtr->rearPtr = newNodePtr; queuePtr->count = 1; } else { queuePtr->rearPtr->nextPtr = newNodePtr; queuePtr->rearPtr = newNodePtr; queuePtr->count++; } 16

17 Serve frontPtr rearPtr frontPtr

18 Serve check that the queue is not empty remember the item in the front node remember the address of the front node make the next node the new front free the old front node decrement count if queue is now empty, set rear to NULL return the item

float serve(Queue* queuePtr) { float item; Node* oldNodePtr = queuePtr->frontPtr; if (queueEmpty(queuePtr)) { fprintf(stderr, “Queue is empty\n”); exit(1); } else { item = oldNodePtr->value; queuePtr->frontPtr = oldNodePtr->nextPtr; queuePtr->count--; free(oldNodePtr); if (queuePtr->count == 0) { queuePtr->rearPtr = NULL; } return item; } 19

#include #include “linkedqueue.h” main() { Queue theQueue; float item; initializeQueue(&theQueue); while (scanf(“%f”, &item) != EOF) { append(&theQueue, item); } while (!queueEmpty(&theQueue)) { item = serve(&theQueue); printf(“%f\n”, item); } 20

21 Revision Linked Stack. –Initialize, Pop, Push. Linked Queue. –Initialize, Append, Serve. Revision: Reading Kruse 3.1.6, 4.6 Standish 7.6, 7.8 Preparation Next lecture: Linked Lists Read in Kruse et al.