Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
§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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures and Algorithms
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
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.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
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.
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.
LINKED LISTS.
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.
CSCE 3110 Data Structures & Algorithm Analysis
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Data Structure By Amee Trivedi.
More on Linked Lists, Stacks, and Queues
Chapter 12 – Data Structures
Chapter 4 The easy stuff.
Pointers and Linked Lists
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Linked Lists Chapter 6 Section 6.4 – 6.6
12 C Data Structures.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stacks.
Chapter 4 Linked Lists.
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Abstract Data Types (ADTs)
Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Object Oriented Programming COP3330 / CGS5409
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Data Structures and Algorithms
Review & Lab assignments
Data Structures and Algorithms
Linked Lists Chapter 4.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Data Structures and Algorithms
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.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Dynamic allocation (continued)
Data Structures and Algorithms
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Data Structures and Algorithms Linked Lists Stacks PLSD210(ii)

Array Limitations Arrays Simple, Fast but Must specify size at construction time Murphy’s law Construct an array with space for n n = twice your estimate of largest collection Tomorrow you’ll need n+1 More flexible system?

Linked Lists Linked list Flexible space use Dynamically allocate space for each element as needed Include a pointer to the next item Linked list Each node of the list contains the data item (an object pointer in our ADT) a pointer to the next node Data Next object

Linked Lists Collection structure has a pointer to the list head Initially NULL Collection Head

Linked Lists Collection structure has a pointer to the list head Initially NULL Add first item Allocate space for node Set its data pointer to object Set Next to NULL Set Head to point to new node Collection node Head Data Next object

Linked Lists Add second item Allocate space for node Set its data pointer to object Set Next to current Head Set Head to point to new node Collection Head node node Data Next object2 Data Next object

Linked Lists - Add implementation struct t_node { void *item; struct t_node *next; } node; typedef struct t_node *Node; struct collection { Node head; …… }; int AddToCollection( Collection c, void *item ) { Node new = malloc( sizeof( struct t_node ) ); new->item = item; new->next = c->head; c->head = new; return TRUE; }

Linked Lists - Add implementation struct t_node { void *item; struct t_node *next; } node; typedef struct t_node *Node; struct collection { Node head; …… }; int AddToCollection( Collection c, void *item ) { Node new = malloc( sizeof( struct t_node ) ); new->item = item; new->next = c->head; c->head = new; return TRUE; } Recursive type definition - C allows it! Error checking, asserts omitted for clarity!

Linked Lists Add time Search time Constant - independent of n Worst case - n Collection Head node node Data Next object2 Data Next object

Linked Lists - Find implementation void *FindinCollection( Collection c, void *key ) { Node n = c->head; while ( n != NULL ) { if ( KeyCmp( ItemKey( n->item ), key ) == 0 ) { return n->item; n = n->next; } return NULL; A recursive implementation is also possible!

Linked Lists - Delete implementation void *DeleteFromCollection( Collection c, void *key ) { Node n, prev; n = prev = c->head; while ( n != NULL ) { if ( KeyCmp( ItemKey( n->item ), key ) == 0 ) { prev->next = n->next; return n; } prev = n; n = n->next; return NULL; head

Linked Lists - Delete implementation void *DeleteFromCollection( Collection c, void *key ) { Node n, prev; n = prev = c->head; while ( n != NULL ) { if ( KeyCmp( ItemKey( n->item ), key ) == 0 ) { prev->next = n->next; return n; } prev = n; n = n->next; return NULL; head Minor addition needed to allow for deleting this one! An exercise!

Linked Lists - LIFO and FIFO Simplest implementation Add to head Last-In-First-Out (LIFO) semantics Modifications First-In-First-Out (FIFO) Keep a tail pointer head struct t_node { void *item; struct t_node *next; } node; typedef struct t_node *Node; struct collection { Node head, tail; }; tail tail is set in the AddToCollection method if head == NULL

Linked Lists - Doubly linked Doubly linked lists Can be scanned in both directions struct t_node { void *item; struct t_node *prev, *next; } node; typedef struct t_node *Node; struct collection { Node head, tail; }; head prev prev prev tail

Stacks Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add item to the top of the stack void *pop( Stack s ); - remove an item from the top of the stack Like a plate stacker Other methods int IsEmpty( Stack s ); /* Return TRUE if empty */ void *Top( Stack s ); /* Return the item at the top, without deleting it */

Stacks - Implementation Arrays Provide a stack capacity to the constructor Flexibility limited but matches many real uses Capacity limited by some constraint Memory in your computer Size of the plate stacker, etc push, pop methods Variants of AddToC…, DeleteFromC… Linked list also possible Stack: basically a Collection with special semantics!

Stacks - Relevance Stacks appear in computer programs Stack frame Key to call / return in functions & procedures Stack frame allows recursive calls Call: push stack frame Return: pop stack frame Stack frame Function arguments Return address Local variables

Stacks - Implementation Arrays common Provide a stack capacity to the constructor Flexibility limited but matches many real uses Stack created with limited capacity struct t_node { void *item; struct t_node *prev, *next; } node; typedef struct t_node *Node; struct collection { Node head, tail; }; prev is optional! head prev prev prev tail