Pointers and Linked Lists

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Stacks, Queues, and Linked Lists
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
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.
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.
Summary of lectures (1 to 11)
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 7 Stacks II CS Data Structures I COSC 2006
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Copyright © 2012 Pearson Education, Inc. 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,
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’
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks And Queues Chapter 18.
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.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
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.
Data Structures Using C++ 2E
Popping Items Off a Stack Using a Function Lesson xx
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
CC 215 Data Structures Queue ADT
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.
Chapter 15 Lists Objectives
CC 215 Data Structures Stack ADT
Cinda Heeren / Geoffrey Tien
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 16-2 Linked Structures
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Popping Items Off a Stack Lesson xx
Pointers and Linked Lists
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.
Recall: stacks and queues
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Stack Implementations
Presentation transcript:

Pointers and Linked Lists Chapter 13 Pointers and Linked Lists

Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues

13.2 Stacks and Queues

A Linked List Application A stack is a data structure that retrieves data in the reverse order the data was stored If 'A', 'B', and then 'C' are placed in a stack, they will be removed in the order 'C', 'B', and then 'A' A stack is a last-in/first-out data structure like the stack of plates in a cafeteria; adding a plate pushes down the stack and the top plate is the first one removed Display 13.16

Program Example: A Stack Class We will create a stack class to store characters Adding an item to a stack is pushing onto the stack Member function push will perform this task Removing an item from the stack is popping the the item off the stack Member function pop will perform this task contains the stack class interface Display 13.17

Using the stack Class demonstrates the use of the stack class Display 13.18 (1-2)

Function push The push function adds an item to the stack It uses a parameter of the type stored in the stack void push(char the_symbol); Pushing an item onto the stack is precisely the same task accomplished by function head_insert of the linked list For a stack, a pointer named top is used instead of a pointer named head

Function pop The pop function returns the item that was at the top of the stack char pop( ); Before popping an item from a stack, pop checks that the stack is not empty pop stores the top item in a local variable result, and the item is "popped" by: top = top->link; A temporary pointer must point to the old top item so it can be "deleted" to prevent a memory leak pop then returns variable result

Empty Stack An empty stack is identified by setting the top pointer to NULL top = NULL;

The Copy Constructor Because the stack class uses a pointer and creates new nodes using new, a copy constructor is needed The copy constructor (a self-test exercise) must make a copy of each item in the stack and store the copies in a new stack Items in the new stack must be in the same position in the stack as in the original

The stack destructor Because function pop calls delete each time an item is popped off the stack, ~stack only needs to call pop until the stack is empty char next; while( ! empty ( ) ) { next = pop( ); }

stack Class Implementation The stack class implementation is found in Display 13.19 (1) Display 13.19 (2)

A Queue A queue is a data structure that retrieves data in the same order the data was stored If 'A', 'B', and then 'C' are placed in a queue, they will be removed in the order ‘A', 'B', and then ‘C' A queue is a first-in/first-out data structure like the checkout line in a supermarket Display 13.20

Queue Class Implementation The queue class is implemented in a manner similar to the stack except there are two pointers, one to track the front of the list, and one to track the back of the list Interface: Program using the Queue Class: Implementation: Display 13.21 (1-2) Display 13.22 (1-2) Display 13.23 (1-3)

Section 13.2 Conclusion Can you Give the definition of member function push? Create a definition for the stack class copy constructor? Know when to use a queue vs. a stack? Create a definition for the queue class copy constructor?

Chapter 13 -- End

Display 13.1 Back Next

Display 13.2 Back Next

Display 13.3 Back Next

Display 13.4 Back Next

Display 13.5 Back Next

Display 13.6 Back Next

Display 13.7 Back Next

Display 13.8 Back Next

Display 13.9 Back Next

Display 13.10 Back Next

Display 13.11 Back Next

Display 13.12 Back Next

Display 13.13 Back Next

Display 13.14 (1/2) Back Next

Display 13.14 (2/2) Back Next

Display 13.15 (1/3) Back Next

Display 13.15 (2/3) Back Next

Display 13.15 (3/3) Back Next

Display 13.16 Back Next

Display 13.17 Back Next

Display 13.18 (1/2) Back Next

Display 13.18 (2/2) Back Next

Display 13.19 (1/2) Back Next

Display 13.19 (2/2) Back Next

Display 13.20 Back Next

Display 13.21 (1/2) Back Next

Display 13.21 (2/2) Back Next

Display 13.22 (1/2) Back Next

Display 13.22 (2/2) Back Next

Display 13.23 (1/3) Back Next

Display 13.23 (2/3) Back Next

Display 13.23 (3/3) Back Next