Summary on linked lists

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

Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
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.
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).
Stacks and Queues.
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.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
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.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
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.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
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.
Algorithms and Data Structures
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
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.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
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.
Chapter 16: Linked Lists.
Programming Circular Linked List.
C++ Programming:. Program Design Including
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Chapter 4 Linked Lists.
Algorithm for deleting a node from a singly linked list
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists Chapter 4.
Recursion.
Chapter 16-2 Linked Structures
as an abstract data structure and
Chapter 4 Linked Lists.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Lists, Stacks and Queues
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Data Structures and Algorithms
Chapter 4 Linked Lists.
Linked Lists.
General List.
Classes with dynamic members
List as an Abstract Data Type
Linked Lists.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
Data Structures & Programming
Presentation transcript:

Summary on linked lists Definition: struct Node{ int data; Node* next; }; typedef Node* NodePtr; NodePtr head; Head 20 45 75 85

(unsorted) linked lists bool listEmpty(NodePtr head) { return (head==NULL); } int getHead(NodePtr head) { if (head != NULL) return head->data; else {cout << “Error” << endl; exit(1);}; NodePtr getRest(NodePtr head) { NodePtr p=NULL; if (head != NULL) p=head->next; return p; NodePtr addHead(NodePtr head, int newdata) { // or: void addHead(NodePtr& head, int newdata) { NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = head; return newPtr; // or: head = newPtr; NodePtr delHead(NodePtr Head){ // or: void delHead(NodePtr& head) if(head != NULL){ NodePtr cur = head; head = head->next; delete cur; return head; // no return for ‘void delHead()’ Operations on (unsorted) linked lists (! listEmpty())

Operations on sorted linked lists: bool listEmpty(NodePtr head) { ... } NodePtr insertNode(NodePtr head, int item) { // or: void insertNode(NodePtr& head, int item){ NodePtr deleteNode(NodePtr head, int item) { // or: void deleteNode(NodePtr& head, int item){ (finding the right position should be careful in implementation!)

If the position happens to be the head NodePtr insertNode(NodePtr head, int item){ NodePtr newp, cur, pre; newp = new Node; newp->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 newp->next = head; head = newp; } else { pre->next = newp; new->next = cur; return head; If the position happens to be the head General case

NodePtr deleteNode(NodePtr head, int item){ NodePtr prev=NULL, cur = head; while( (cur!=NULL) && (item > cur->data)){ prev = cur; cur = cur->next; } if ( cur!==NULL && cur->data==item) { if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; return head;

Some simple algorithms Write a function that returns the length of a given list. Write a boolean function that tests whether a given unsorted list of characters is a palindrome. Write a function that computes the union of two sorted linked lists of integers.

The length of a given list: int length(NodePtr head) { NodePtr cur = head; int l=0; while(cur != NULL){ l++; cur = cur->next; } return l;

Recursive version: Or in functions: deleteHead int length(NodePtr head) { int l; if(head==NULL) l=0; else l=length(head->next)+1; return l; } Or in functions: int length(NodePtr head) { int l; if(listEmpty(head)) l=0; else l=length(getRest(head))+1; return l; } deleteHead

Everything is recursive with lists: recursively print a list: void print(NodePtr head) { if(head != NULL){ cout << head->data; print(head->next); }

Test if the given list is a palindrome: [a b c d d c b a] is a palindrome, [a b c d c] is not. bool isPalindrome(NodePtr head) { 1. create a new list in inverse order, newList 2. check the two lists, head and newList, whether they are the same }

Create a new list in the inverse order newHead is the pointer to the new list! NodePtr newHead=NULL; NodePtr p=Head; while(p != NULL) { addHead(newHead,p->data); // or: newHead=addHead(newHead,p->data); p=p->next; }

Check wheter two lists are the same: NodePtr p1 = head; NodePtr p2 = newList; bool palindrome=true; while((p1 != NULL) && (palindrome) ){ if ((p1->data)==(p2->data)) { p1=p1->next; p2=p2->next; } else palindrome=false; return palindrome;

Remove the newList properly! bool isPalindrome(NodePtr head) { NodePtr newList=NULL; NodePtr p=head; while(p != NULL) { addHead(newList,p->data); p=p->next; } NodePtr p1=head; NodePtr p2=newList; bool palindrome=true; while((p1 != NULL) && (palindrome) ){ if ((p1->data)==(p2->data)) { p1=p1->next; p2=p2->next; else palindrome=false; p=newList; while (p!=NULL) { delHead(p); return palindrome; Create the new list Test the two lists Remove the newList properly!

To do this, we need to use the functional definition of addEnd: Do it recursively To do this, we need to use the functional definition of addEnd: NodePtr addEnd(NodePtr Head, int item) bool isPalindrome(NodePtr head) { bool palin; NodePtr newHead=reverse(head); palin=equal(head, newHead); deleteList(newHead); return palin; }

NodePtr reverse(NodePtr head) { NodePtr res; if (head==NULL) res=NULL; else res=addEnd(reverse(head->next),head->data); return res; }

bool equal(NodePtr p1, NodePtr p2) { bool equality; if (p1==NULL) equality = true; else if ((p1->data)!=(p2->data)) equality = false; else equality=palindrome(p1->next,p2->next); return equality; }

void deleteList(NodePtr head) { NodePtr p=head; if (p!=NULL) { p=deleteHead(p); deleteList(p); }

Union of two sorted lists: given two sorted linked lists, create a new sorted list that is the union of the two. union ([1, 2, 4, 5], [3, 4, 5, 6, 7]) gives [1, 2, 3, 4, 5, 6, 7]

NodePtr union(NodePtr p1, NodePtr p2) { // look at the frist list p1 while (p1 is not empty) { just copy it! } // simply: unionList = p1; // start from p1 // look at the second list p2 while(p2 is not empty){ if the first element of p2 is not in the current union, then add it into the union list otherwise, move forward the list return unionL;

NodePtr union(NodePtr p1, NodePtr p2) { NodePtr unionL, p; if (p1==NULL) unionL=p2; else if (p2==NULL) unionL=p1; else { unionL=p1; p=p2; while((p != NULL) ){ if (!searchNode(unionL, p->data)) insertNode(unionL,p->data); p=p->next; } return unionL;