1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
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.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
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.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
1 CSC 222: Computer Programming II Spring 2004 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search Class design  templated.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
C++ Classes and Data Structures Jeffrey S. Childs
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
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,
Pointers OVERVIEW.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1 CSC 222: Computer Programming II Spring 2004 Pointers and dynamic memory  pointer type  dereference operator (*), address-of operator (&)  sorting.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stack.
Chapter 20: Binary Trees.
Pointers and Linked Lists
Function and class templates
Chapter 21: Binary Trees.
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Introduction to C++ Linear Linked Lists
Pointers & Dynamic Data Structures
Data Structures & Algorithms
CSI 1340 Introduction to Computer Science II
Dynamic allocation (continued)
CSCS-200 Data Structure and Algorithms
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node class  linked lists vs. vectors  stack & queue implementations

2 List implementations in List, SortedList, stack, queue, …  we needed to store a sequence of items  if size is known ahead of time, can use an array  better (more flexible) choice is vector  create with initial size; if need more space, double size  note: additions not constant time, may result in half the space being wasted yet another alternative: linked list  allocate space for each new item as needed  deallocate space when no longer needed  want constant time additions/deletions, but not necessarily direct access

3 In-class exercise class Person { public: int leftHand; Person * rightHand; } idea: chain people together  left hand stores data (here, an integer)  right hand points to the next person in the chain to construct a chain…  need a pointer ( Person * ) to the front of the chain  the end of the list must be clear – use a special pointer (NULL) to mark the end ADD TO FRONT, DELETE FROM FRONT, DISPLAY ALL, SEARCH, …

4 Linked list code Person * list = NULL; attempting to dereference a NULL pointer results in a run-time error list to add to the front of an empty list: list = new Person;// allocate new Person & // make list point to it (*list).leftHand = 3;// store 3 in left hand (*list).rightHand = NULL;// store NULL in right hand parenthesization is ugly – better notation: list->leftHand = 3;// -> is shorthand for list->rightHand = NULL;// dereference then access

5 Displaying the contents of a linked list list naïve attempt: while (list != NULL) { cout leftHand << endl; list = list->rightHand; } if you lose the only pointer to a node, it becomes inaccessible! better: Person * step = list; while (step != NULL) { cout leftHand << endl; list = list->rightHand; } 654 PROBLEMS? or could use a for loop

6 Adding to the front list in general, adding to the front of an arbitrary list: Person * temp = new Person;// allocate new Person temp->leftHand = 3;// store the data temp->rightHand = list;// make new Person point // to the old front list = temp;// make new Person the // front 654

7 Deleting from the front list naïve attempt: list = list->rightHand; 654 PROBLEMS? memory leak! better: Person * temp = list; list = list->rightHand; delete temp; STILL HAS POTENTIAL PROBLEMS what are they? fixes?

8 Adding/deleting in the middle list must find the location (traverse from the front)  to delete a node from the middle, need a pointer to the node before it. Person * temp = previous->rightHand; previous->rightHand = temp->rightHand; delete temp; 654  to add a node in the middle, need a pointer to the node before it. Person * temp = new Person; temp->leftHand = ???; temp->rightHand = previous->rightHand; previous->rightHand = temp;

9 Node class template class Node { public: Node (Item item, Node * ptr = NULL) { value = item; next = ptr; } void setValue(Item item) { value = item; } void setNext(Node * ptr) { next = ptr; } Item getValue() { return value; } Node * getNext() { return next; } private: Item value; Node * next; }; can define a generic Node class – useful for constructing any linked list structures Node * list = new Node (12, NULL); Node * temp = new Node (9, list); list = temp; Node * second = list->getNext(); second->setNext(new Node (100, NULL));

10 Linked lists vs. vector advantages of linked lists  no empty entries – uses space proportional to the # of items in the list  once the place is found, can add/delete in constant time  unlike resizing, all adds/deletes require equal time disadvantages of linked lists  each node wastes space (extra pointer field)  sequential access only – no direct access, can traverse in 1 direction only there are applications where linked lists are clear winners  stack: no need to search for location – add/delete at one end  queue: no need to search for location – add at one end, delete at other

11 Implementing stack using a linked list data fields: Node * stkTop; int numItems; stkTop 654 void push(Type item) { Node * newNode = new Node (item, stkTop); stkTop = newNode; numItems++; } void pop() { if (!empty()) { Node * temp = stkTop; stkTop = stkTop->getNext(); delete temp; numItems--; } Type top() const { return stkTop->getValue(); } bool empty() const { return (size() == 0); } int size() const { return numItems; }

12 Stack implementation template class stack { public: stack () { /* CONSTRUCTOR */ } stack (const stack & other) { /* COPY CONSTRUCTOR */ } ~stack () { /* DESTRUCTOR */ } stack & operator=(const stack & other) { /* ASSIGNMENT OPERATOR */ } void push(Type item) { /* … */ } void pop() { /* … */ } Type top() const { /* … */ } bool empty() const { /* … */ } int size() const { /* … */ } private: template class Node { /* CLASS DEFINITION */ }; Node * stkTop; int numItems; Node * copyList(Node * ptr) const { /* HELPER */ } }; note: Node class is defined in the private section – not even visible to client program view full source code

13 Implementing queue using a linked list data fields: Node * qFront; Node * qBack; int numItems; qFront 654 void push(Type item) { if (empty()) { qBack = new Node (item, NULL); qFront = qBack; } else { qBack->setNext(new Node (item, NULL)); qBack = qBack->getNext(); } numItems++; } void pop() { if (!empty()) { Node * temp = qFront; qFront = qFront->getNext(); delete temp; if (qFront == NULL) { qBack == NULL; } numItems--; } qBack which end is which? does it matter?

14 Queue implementation template class queue { public: queue () { /* CONSTRUCTOR */ } queue (const queue & other) { /* COPY CONSTRUCTOR */ } ~queue () { /* DESTRUCTOR */ } queue & operator=(const queue & other) { /* ASSIGNMENT OPERATOR */ } void push(Type item) { /* … */ } void pop() { /* … */ } Type front() const { /* … */ } bool empty() const { /* … */ } int size() const { /* … */ } private: template class Node { /* CLASS DEFINITION */ }; Node * qFront; Node * qBack; int numItems; Node * copyList(Node * ptr) const { /* HELPER */ } }; again: Node class is defined in the private section – not even visible to client program view full source code

15 Variations on linked lists can add extra links to make linked lists more flexible  doubly-linked list has links going both ways – can traverse either direction  circularly-linked list has link connecting end back to front – can go around  can combine and have doubly- and circularly-linked list  non-linear list can have structure e.g., binary search tree in CSC427, we will develop your programming/design skills further  more complex structures for storing and accessing information  algorithms for solving more complex problems  problem-solving approaches "boo" "baz""foo" "bar""biz""fiz""zoo"

16 FINAL EXAM similar format to previous tests  true or false  discussion/short answer  explain/modify/write code cumulative, but will emphasize material since Test 2 designed to take minutes, will allow full 100 minutes study hints:  review lecture notes  review text  look for supplementary materials where needed (e.g., Web search)  think big picture -- assimilate the material!  use online review sheet as a study guide, but not exhaustivereview sheet