Stacks, Queues, and Linked Lists

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
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.”
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Summary of lectures (1 to 11)
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.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Stack Queue.
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.
Algorithms and Data Structures Representing Sequences by Arrays and Linked Lists.
DS.L.1 Lists, Stacks, and Queues (Review) Chapter 3 Overview Abstract Data Types Linked Lists, Headers, Circular Links Cursor (Array) Implementation Stacks.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
Information and Computer Sciences University of Hawaii, Manoa
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
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,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
CSE 1342 Programming Concepts
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
Program based on queue & their operations for an application
Week 3 - Friday CS221.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
ADT list.
Review & Lab assignments
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks CS-240 Dick Steflik.
Circular Queues: Implemented using Arrays
Presentation transcript:

Stacks, Queues, and Linked Lists Stacks (Last in/First out List) Operations: Push, Pop, Test Empty, Test Full, Peek, Size Queue(First in/First out List) Operations: Insert, Remove, Test Empty, Test Full, Peek, Size Linked List(A list of elements connected by pointers) Insert, Delete, Find, Traverse, Size Advantages: can grow, delete/insert with assignments

Complexities Queue Stack: List Insert O(1) Remove O(1) Peek O(1) isEmpty O(1) isFull O(1) List Search O(N) Stack: Push O(1) Pop O(1) Peek O(1) isEmpty O(1) isFull O(1)

Stack Implementation With an array With a linked list Need stack array and top members. Push: (top<MAX) ? stack[++top] = data : <error>; Pop: (top>0) ? return stack[--top]:<error>; With a linked list Push: insert at front of list. Pop: remove from front of list. What are the complexities?

Stack: Array Implementation int top, arraySize; Data *array[]; void Stack(int s) { array = malloc(sizeof(Data)*s; arraySize = s; top = -1; } int push(Data *s) { if (isFull()) return false; array[++top] =s; return true; } Data *pop() { if (isEmpty()) return null; return array[top--]; } int isEmpty() { return (top < 0); } int isFull() { return top+1 == arraySize; } Data *peek() { if (isEmpty()) return null; return array[top];} Note: In C, non-zero = true, zero = false

Stack: Linked List Implementation Data *top; Stack() { top = NULL; } void push(Data* d) { d->link=top; top=d; } Data *Pop() { if (isEmpty()) return null; Data *value = top; top = value->link; return value; } int isEmpty() {return (top == NULL);} Data *peek() { return top; }

Queue Implementation With an array With a linked list Circular Queue Need queue array, size, head, and tail pointers. Insert: (size<MAX) ? queue[++tail%MAX] = data : <error>; Remove: (size > 0) ? return queue[++head%MAX] : <error>; With a linked list Insert: Insert to back of queue Remove: Remove from front of queue. What are the complexities?

Queue: Array Implementation int head, tail, entries; int size; Data *array[]; public Queue(int s) { array = malloc(sizeof(Data)*s); size = s; head =0; tail = -1; entries = 0;} public boolean insert(Data s) { if (isFull()) return 0; if (tail==size) tail = -1; array[++tail] = s; entries++; return 1; } public Data *remove() { if (isEmpty()) return NULL; Data *temp = array[head]; head = (head+1)%size; entries--; return temp; } int isEmpty() { return entries == 0; } Data *peek() { if (isEmpty() return 0; return array[head]; } }

Queue: Linked List Implementation Data *head, *tail; Queue(int s) { head =null; tail = null; } int Insert(Data *s) { if (isEmpty()) { head = tail = s } else { last->link = value; tail= s; } return true; } Data *Remove() { Data *value = head; if (isEmpty()) return NULL; head = head->link; if (head == NULL) : tail = NULL; return value; } int isEmpty() { return (head == null; } Data peek() { return head; }

Linked List Implementation See Text Example With dynamic memory The data structure uses object links. Insert/Delete: Search; assignments to change pointers With an array (Use indices instead of data links) Need to list array, size, and pointer to initial entry. Initialization: Create free entry chain. Insert: retrieve from free list if any and do normal insertion. Delete: Do normal insertion logic and then add to free list. The data structure uses primitive links rather than object links.

Ordered Linked List Insertion Item first; void insert(Item *d ) { Item *previous = null; Item *current = first; while (current!=NULL && d->key > current->key) { previous=current; current=current->next); } if (previous == NULL) first = d; else previous->next = d; d->next = current; } Note: Duplicates OK in this implementation

Linked List Removal Item remove(Item *d) { Item *current = first; *previous = NULL; do { if (current == null) return NULL; if (!equals(current->key, d->key)) { previous = current; current = current->next; } } while (current!=null && !equals(current->key, d->key)) if (previous == NULL) first = first->next; else previous->next = current->next; return current; }

Doubly Linked List See Text Example Two links. One to next record and one to previous. Characteristics. More assignments to maintain links. Don’t need the previous temporary pointer. More memory per record. More secure. Used for operating systems.

Doubly Linked Insertion void insert( Item *d ) { Item *current = first, *previous = NULL; while (current!=NULL&&compareTo(d->key, current->key)<0) { previous = current; current=current->next); } if (current == NULL) { if (previous == NULL) first = current; else previous->next = d; } else { if (current->previous==NULL) { first=d; d->next=current; current->previous=d;} else { d->previous = current->previous; d->next = current; current->previous->next = d; current->previous = d; }

Doubly Linked Removal int remove(Item *d) { Item *current = first; do { if (current == NULL) return NULL; if (current->key != d->key) {current = current->next; } } while (!equals(current->key, d->key)); if (current->previous == NULL) first = current->next; else current->previous->next = current->next; if (current->next != NULL) current->next->previous = current->previous; return true; }

Stack Examples Matching pairs of delimiters Evaluating infix expressions Two stacks First convert to postfix and then evaluate Expression examples {…{….[..{.<.{..{…}…}…>..].}..} 500/(1+2*(3+4*5/2))*(3*2+1)