Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Classes with dynamic members. int x; void f() { x=10; } main() { x=0; f(); } ‘Class’ matters! int x; void f() { int x; x=10; } main() { x=0; f(); } class.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
(Walls & Mirrors - Beginning of Chapter 4)
Abstract Data Type: List (optional, not required).
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Classes with dynamic objects List as a (dynamic) class.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
COMP104 Linked List Algorithms / Slide 1 Some Simple Algorithms on Linked Lists * Write a function that returns the length of a given list. * Write a boolean.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Abstract Data Type (ADT) &List (Part II) Lecture Objectives Implement linked list using pointers Concept of iterators STL's list class Other variations.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
CSCE 210 Data Structures and Algorithms
Programming Circular Linked List.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists.
Linked lists.
Chapter 4 Linked Lists
Linked Lists Chapter 4.
Summary on linked lists
Chapter 16-2 Linked Structures
as an abstract data structure and
Chapter 4 Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
Classes with dynamic members
List as an Abstract Data Type
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked lists.
Presentation transcript:

Linked Lists

COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked list n Stores one piece of data n Points to the next element or NULL. * Linked lists provide natural implementation. * A linked list of integers:

COMP104 Lecture 33 / Slide 3 * Original linked list of integers: * Insertion by pointers: * Deletion by pointers: Linked Lists: Operations old value deleted item

COMP104 Lecture 33 / Slide 4 Pointer Based Linked Lists * Linked lists store data in increasing order: * Definitions: 1. Defining a node (recursive datatype - use itself to define itself!) class node { public: int data; node* next; }; // end class typedef node* ptrType; // ptrType is a pointer to a node

COMP104 Lecture 33 / Slide 5 typedef  typedef allows you to make new shortcuts to existing types typedef int FunnyInt; FunnyInt k;// same as:int k; typedef int* IntPtr; IntPtr p;// same as: int *p; p = new node; *p = 100; 100 p

COMP104 Lecture 33 / Slide 6 Pointer Based Linked Lists * Definitions (cont’d): 2. To create/delete node P = new node; delete P; 3. The fields in P can be accessed using: P->data = 100;and P->next = NULL; Alternatively, we could also write: (*P).data = 100;and (*P).next = NULL; 100 p

COMP104 Lecture 33 / Slide 7 Accessing Linked Lists * We define a pointer ptrType Head; that points to the first node of the linked list. When the linked list is empty (empty list) then Head is NULL Head

COMP104 Lecture 33 / Slide 8 Traversing a Linked List * A linked list is displayed by visiting its nodes one by one, and displaying their data fields. void DisplayList(ptrType Head) { ptrType current = Head; while(current != NULL){ cout data << endl; current = current->next; }

COMP104 Lecture 33 / Slide 9 Traversing a Linked List * Alternatively, we can use for loop to traverse a linked list: void DisplayList(ptrType Head){ ptrType cur; for (cur = Head; cur != NULL; cur = cur->next) { cout data << endl; }

COMP104 Lecture 33 / Slide 10 Displaying a Linked List current = Head; current = current->next; 2045 Head current 2045 Head current

COMP104 Lecture 33 / Slide 11 (to delete) Deleting a Node * To delete a node from the list 1. Locate the node to be deleted (a) cur points to the node to be deleted. (b) prev points to cur ’s predecessor 2. Disconnect node from list using: prev->next = cur->next; 3. Return deleted node to system: delete cur; Head cur prev...

COMP104 Lecture 33 / Slide 12 (to delete) Deleting the Head Node * When the node to be deleted is the first node, there is no predecessor node. Use: Head = Head->next; instead of: prev->next = cur->next; Head cur

COMP104 Lecture 33 / Slide 13 Inserting a Node * To insert a new node into the list 1. (a) Create a new node using: newPtr = new node; (b) Fill in the data field correctly. 2. Find “prev” and “cur” such that the new node should be inserted between *prev (the node being pointed at by prev ) and *cur. 3. Connect the new node to the list by using: (a) newPtr->next = cur; (b) prev->next = newPtr;

COMP104 Lecture 33 / Slide 14 Inserting a Node Head (b) prev->next = newPtr; curprev 2. find prev and cur 33 newPtr 1. newPtr = new node 3(a) newPtr->next = cur ;

COMP104 Lecture 33 / Slide 15 Inserting a Node at the Beginning * When the new node is to be inserted at the beginning of the list there is no predecessor node. Use: newPtr->next = Head; Head = newPtr; instead of: newPtr->next = cur; prev->next = newPtr; Head newPtr cur newPtr->next = Head; Head = newPtr;

COMP104 Lecture 33 / Slide 16 Finding prev and cur * In previous slides we assumed that prev and cur were known. How did we find them?  Suppose that we want to insert or delete a node with data value newValue. Then the following code successfully finds prev and cur. prev = NULL; cur = Head; while(cur!=NULL && newValue > cur->data){ prev = cur; cur = cur->next; }

COMP104 Lecture 33 / Slide 17 Finding prev and cur * Insertion Example with newValue=60:... Head prev = NULL; cur = Head; while(cur!=NULL && newValue > cur->data){ prev = cur; cur = cur->next; } cur prev curprevcurprev

COMP104 Lecture 33 / Slide 18 Passing a Linked List to a Function  When passing a linked list to a function it should suffice to pass the value of Head. Using the value of Head the function can access the entire list.  Problem: If a function changes the beginning of a list by inserting or deleting a node, then Head will no longer point to the beginning of the list.  Solution: When passing Head always pass it by reference Head

COMP104 Lecture 33 / Slide 19 Pointer Based Linked Lists * We now describe how to implement a Linked-List ADT (Abstract Data Type) using pointers. * Main Points: Contains a “Size” field and a “Head” pointing to the list. n Implemented as a Class. n Uses “Deep Copy” in the copy-constructor to copy the entire data structure. n Destructor deallocates dynamic memory of object Head 4 Size

#include using namespace std; class listNode{ // a node on the list public: int data;// a data item on the list listNode *next; // pointer to next node }; typedef listNode* ptrType; // pointer to node

class listClass { public: // constructors and destructor: listClass(); listClass(const listClass& L); ~listClass(); // list operations: bool empty(); int length(); void add(int newdata); void del(int deldata); void print(); private: int Size; ptrType Head; ptrType new_and_check(); };

ptrType listClass::new_and_check() { // allocate a new node; returns only upon successful // memory allocation; else, exit ptrType temp; temp = new listNode; if (temp == NULL) { // not successful cerr << "Memory allocation error!" << endl; exit(1); } return temp; }

listClass::listClass(){ Size = 0; Head = NULL; } listClass::~listClass(){ while(Head!=NULL){ ptrType cur = Head; Head = Head->next; delete cur; } bool listClass::empty(){ if(Size==0) return true; else return false; } int listClass::length(){ return Size; }

listClass::listClass(const listClass& L){ Size = L.Size; if(L.Head==NULL){ Head = NULL; // empty list return; } Head = new_and_check (); // copy first node Head->data = L.Head->data; ptrType cur = Head;// new list pointer ptrType orig = L.Head->next; while(orig != NULL) { cur->next = new_and_check(); cur = cur->next; cur->data = orig->data; orig = orig->next; } cur->next = NULL; }

void listClass::add(int newdata){ Size++; ptrType newPtr = new listNode; if(newPtr==NULL){ cerr << "Memory error" << endl; exit(1); } newPtr->data = newdata; newPtr->next = NULL; ptrType prev, cur = Head; while(cur!=NULL && newdata > cur->data){ prev = cur; cur = cur->next; } if(cur==Head){ newPtr->next = Head; Head = newPtr; } else{ newPtr->next = cur; prev->next = newPtr; }

void listClass::del(int deldata){ ptrType prev, cur = Head; while(cur!=NULL && deldata > cur->data){ prev = cur; cur = cur->next; } if(cur==NULL || cur->data!=deldata){ cout << "Delete error: " << deldata << " not in list!" << endl; return; } if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; Size--; }

void listClass::print(){ cout << "[ "; ptrType cur = Head; while(cur != NULL){ cout data << " "; cur = cur->next; } cout << "]" << endl; }

void main(){ listClass L; L.add(30); L.print(); listClass N(L); N.print(); L.add(20); L.print(); L.add(40); L.print(); L.add(10); L.print(); L.del(50); L.print(); L.del(40); L.print(); L.del(30); L.print(); L.del(20); L.print(); L.del(10); L.print(); }

COMP104 Lecture 33 / Slide 29 Output [ 30 ] [ ] [ ] [ ] Delete error: 50 not in list! [ ] [ ] [ ] [ 10 ] [ ]