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.

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
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.
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.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Concept of lists and array implementations of lists.
1 Linked Lists. 2 Implementing a student package We want to create (part of) a course- management program. We need to – Maintain a list of the participating.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
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.
Abstract Data Type: List (optional, not required).
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
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.
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.
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.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
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.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Abstract Data Type (ADT) &List (Part II) Lecture Objectives Implement linked list using pointers Concept of iterators STL's list class Other variations.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
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.
Programming Circular Linked List.
Chapter 12 – Data Structures
Exercise 1 From Lec80b-Ponter.ppt.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Summary on linked lists
Chapter 16-2 Linked Structures
as an abstract data structure and
Chapter 4 Linked Lists.
Review & Lab assignments
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked Lists.
List as an Abstract Data Type
Linked Lists.
Presentation transcript:

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 arrays for lists * Examples: List of ten students marks int studentMarks[10]; List of temperatures for the last two weeks double temperature[14];

COMP104 Linked Lists / Slide 3 Motivation * list using static array int myArray[10]; We have to decide in advance the size of the array (list) * list using dynamic array int n, *myArray; cin >> n; myArray = new int[n]; We allocate an array (list) of any specified size while the program is running * linked-list (dynamic size) size = ?? The list is dynamic. It can grow and shrink to any size.

COMP104 Linked Lists / Slide 4 Linked Lists: Basic Idea * A linked list is an ordered collection of data * Each element of the linked list has n Some data n A link to the next element * The link is used to chain the data * Example: A linked list of integers: Data Link

COMP104 Linked Lists / Slide 5 * The list can grow and shrink Linked Lists: Basic Ideas add(75), add(85) delete(85), delete(45), delete(20) 75

COMP104 Linked Lists / Slide 6 * Original linked list of integers: * Insertion: * Deletion Linked Lists: Operations old value deleted item

#include using namespace std; struct Node{ int data; Node *next; }; typedef Node* NodePtr;

COMP104 Linked Lists / Slide 8 typedef * typedef allows you to make new shortcuts to existing types typedef int WAH; WAH k;// same as:int k; typedef int* WAHPTR; WAHPTR p;// same as: int *p; typedef Node* NodePtr; NodePtr Head;// same as: Node* Head;

COMP104 Linked Lists / Slide 9 Linked List Structure * Node : Data + Link n Definition struct Node { int data;//contains useful information Node* next;//points to next element or NULL }; n Create a Node Node* p; p = new Node;//points to newly allocated memory n Delete a Node delete p;

COMP104 Linked Lists / Slide 10 Linked List Structures n Access fields in a node (*p).data;//access the data field (*p).next;//access the pointer field Or it can be accessed this way p->data//access the data field p->next//access the pointer field

COMP104 Linked Lists / Slide 11 Representing and accessing Linked Lists * We define a pointer NodePtr Head; that points to the first node of the linked list. When the linked list is empty then Head is NULL Head

COMP104 Linked Lists / Slide 12 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 (or using a function to return a new pointer value) Head

COMP104 Linked Lists / Slide 13 Manipulation of a Unsorted Linked List

COMP104 Linked Lists / Slide 14 Start the first node from scratch NodePtr newPtr; newPtr = new Node; newPtr->data = 20; newPtr->next = NULL; Head = newPtr; Head newPtr 20 Head Head = NULL;

COMP104 Linked Lists / Slide 15 Inserting a Node at the Beginning newPtr = new Node; newPtr->data = 13; newPtr->next = Head; Head = newPtr; Head newPtr 13 20

COMP104 Linked Lists / Slide 16 Keep going … Head newPtr

void addHead(NodePtr& Head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = Head; Head = newPtr; } Adding an element to the head:

COMP104 Linked Lists / Slide 18 (to delete) Deleting the Head Node NodePtr cur; cur = Head; Head = Head->next; delete cur; Head cur

void delHead(NodePtr& Head){ if(Head != NULL){ NodePtr cur = Head; Head = Head->next; delete cur; }

COMP104 Linked Lists / Slide 20 Displaying a Linked List cur = Head; cur = cur->next; 2045 Head cur 2045 Head cur

COMP104 Linked Lists / Slide 21 * A linked list is displayed by walking through its nodes one by one, and displaying their data fields. void DisplayList(NodePtr Head){ NodePtr cur; cur = Head; while(cur != NULL){ cout data << endl; cur = cur->next; }

//return the pointer of the node has data=item //return NULL if item does not exist NodePtr searchNode(NodePtr Head, int item){ NodePtr Cur = Head; NodePtr Result = NULL; while(Cur != NULL){ if(Cur->data == item) Result = Cur; Cur = Cur->next; } return Result; } Searching for a node

COMP104 Linked Lists / Slide 23 * Original linked list of integers: * Add to the end (insert at the end): More operation: adding to the end Last element The key is how to locate the last element or node of the list!

void addEnd(NodePtr& Head, int newdata){ NodePtr last = Head; NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = NULL; if(Head != NULL){ // non-empty list case while(last->next != NULL) last = last->next; last->next = newPtr; } else// deal with the case of empty list Head = newPtr; } Add to the end: Link new object to last->next Link a new object to empty list

COMP104 Linked Lists / Slide 25 Manipulation of a Sorted Linked List

COMP104 Linked Lists / Slide 26 Inserting a Node Head cur prev... newPtr

COMP104 Linked Lists / Slide 27 * To insert a new node into the list 1. (a) Create a new node using: NodePtr 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 and *cur. 3. Connect the new node to the list by using: (a) newPtr->next = cur; (b) prev->next = newPtr;

COMP104 Linked Lists / Slide 28 Finding prev and cur  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; }

//insert item into linked list in ascending order void insertNode(NodePtr& Head, int item){ NodePtr New, Cur, Pre; New = new Node; New->data = item; Pre = NULL; Cur = Head; while(Cur != NULL && item > Cur->data){ Pre = Cur; Cur = Cur->next; } if(Pre == NULL){//insert to head of linked list New->next = Head; Head = New; } else { Pre->next = New; New->next = Cur; }

COMP104 Linked Lists / Slide 30 (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. (b) prev points to its predecessor 2. Disconnect node from list using: prev->next = cur->next; 3. Return deleted node to system: delete cur; Head cur prev...

void deleteNode(NodePtr& Head, int item){ NodePtr prev, cur = Head; while(cur!=NULL && item > cur->data){ prev = cur; cur = cur->next; } if(cur==NULL || cur->data!=item){ cout << "Delete error: " << item << " not in list!" << endl; return; } if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; } Delete an element in a sorted linked list: