CSCI2100B Linked List Jeffrey

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 3: Linked List Tutor: Angie Hui
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Linked Lists Chapter 4.
Queues and Linked Lists
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Chapter 4 Lists Pointers Singly Linked Lists
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Data Structures Using C++
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
Double Linked List Operations Dr. David Tsai 2010/4/12.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010.
CSCE 3110 Data Structures & Algorithm Analysis
LIST PROCESSING.
Double-Linked Lists and Circular Lists
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
§1 Abstract Data Type (ADT)
CSE Lecture 12 – Linked Lists …
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Review Pseudo Code Basic elements of Pseudo code
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
25 seconds left…...
Chapter 9: Data Structures I
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Data Structures Using C++ 2E
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS Data Structures Chapter 4 Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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 &
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Data Structures and Algorithms
Data Structures 7th Week
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Data Structures and Algorithms
Presentation transcript:

CSCI2100B Linked List Jeffrey Yu@CUHK

Problems of Arrays in Implementation Let the base address of an array to be a. The address of the i-th element is located at a + (i - 1) * sizeof(theTypeOfElement). We can access an element in an array in constant time if we know its array index. But, suppose that we have stored four integers (12, 20, 40, 65) in an array and we want to insert an integer 30 as the third-element in the array. What is the cost? 12 20 40 65 insert 30 Linked List

Linked List Unlike an array, with a linked list representation (singly linked list, double linked list, etc.), elements can be placed anywhere in memory. With singly linked list, an element is a “node” which has two parts: A data component. A pointer to the next element in the list. An example typedef struct listNode *listPointer; typedef struct listNode { int data; listPointer link; }; Linked List

A Singly Linked List Example listPointer l 30 data link 12 data link 20 40 65 data link NULL What is the advantage/disadvantage of the singly linked representation in terms of read/update values and insertion/deletion of nodes? Linked List

Implementation of Singly Linked List listPointer create(){ return (listPointer)NULL; } Boolean IsEmptyL(listPointer l){ if (l == NULL) return TRUE; else return FALSE; } int main() { listPointer l; l = create(); ... NULL is used as 0 meaning empty and false in C language. NULL does not have a type. (ListPointer)NULL means that it is a NULL value with the type of ListPointer like (int*)NULL. Linked List

The Length and the nth Position Consider a list. The size of a list is the number of elements. If a list is empty (NULL), the number is zero. The nth position of a list is the n-th element of the list. The first element is at the 0-th position. listPointer l 12 20 40 65 NULL Linked List

The Length and the nth Position listPointer nth(listPointer l, int index){ /* returns the indexed element */ listPionter move = l; while (index > 0) {move = move->link; index--;} return move; } int length(listPointer l){ /* returns the length of list */ listPointer move = l; int i = 0; while (move != NULL) { i++; move = move->link;} return i; } listPointer l 12 20 40 65 NULL Linked List

Singly Linked List: Last listPointer last(listPointer l){ /* returns ptr to the last list element */ listPointer prev, next; if (IsEmptyL(l)) return NULL; prev = l; next = prev->link; while (next != NULL) { prev = next; next = prev->link; } return prev; prev next listPointer l 12 20 40 65 NULL Linked List

Singly Linked List: Prepend listPointer prepend(listPointer l, int e){ /* insert to the front of list */ listPointer tmp; tmp = (listPointer)malloc(sizeof(listNode)); tmp->data = e; if (IsEmptyL(l)) { tmp->link = NULL; return tmp; } else { tmp->link = l; l = tmp; return l; } tmp e 12 20 40 65 NULL listPointer l Linked List

Singly Linked List: Append listPointer append(listPointer l, int e){ /* append an element at the end*/ listPointer end; if (IsEmptyL(l)) return prepend(l, e); end = last(l); end->link = prepend(NULL, e); return l; } listPoniter l end e NULL 12 20 40 65 NULL Linked List

Singly Linked List: Delete Consider deleting an element from a list. The delete procedure is to delete an element from a list l specified by trial, and return the new deleted list. listPointer delete(listPointer l, listPointer trail) There are three cases about trail. To delete the first element (trail == NULL) To delete the last element (trail == last(l)) To delete an element in the middle (trail is the precedent node of the node to be deleted) listPointer l 12 20 40 65 NULL Linked List

Singly Linked List: Delete listPointer delete(listPointer l, listPointer trail){ /* delete from a node from a list at the position specified by trail */ listPointer w, t, p; if (IsEmptyL(trail)) { /* delete the first node */ w = l; l = l->link; } else { t = last(l); if (trail == t) { /* assume the list l is long enough */ w = trail; p = nth(l, length(l) - 2); p->link = NULL; } else { w = trail->link; trail->link = trail->link->link; } } free(w); return l; listPointer l 12 20 40 65 NULL Linked List

Another Set of Operations In the textbook (Section 4.2 -- Chapter 4, Section 2), it offers two operations to insert/delete a node into/from a list. Read Section 4.2. And consider the differences to manipulate lists. Linked List

Dynamically Linked Stack typedef struct { listNode *element; /* For listNode, refer to the slide 4-3 */ } stack; stack *createS(){ stack *s; s = (stack*)malloc(sizeof(stack)); s->element = NULL; return s; } Boolean IsEmptyS(stack *s){return IsEmptyL(s->element);} Linked List

Dynamically Linked Stack void push(stack *s, int e){ s->element = prepend(s->element, e); } int pop(stack *s){ int i; listPointer t; if (!IsEmptyS(s)) { t = nth(s->element, 0); i = t->data; s->element = delete(s->element, NULL); /* delete the first */ return i; } else printf("Error\n"); Linked List

Dynamically Linked Queue typedef struct { listNode *element; /* For listNode, refer to the slide 4-3 */ } queue; queue *createQ(){ queue *q; q = (queue*)malloc(sizeof(queue)); q->element = NULL; return q; } Boolean IsEmptyQ(queue *q){ return IsEmptyL(q->element); Linked List

Dynamically Linked Queue void enqueue(queue *q, int e){ q->element = append(q->element, e); } int dequeue(queue *q){ int i; listNode *t; if (!IsEmptyQ(q)) { t = nth(q->element, 0); i = t->data; q->element = delete(q->element, NULL); return i; } else printf("Error\n"); Linked List

Doubly Linked List Singly linked lists sound good. But, we can only traverse from one to the next, and we cannot traverse from one to its previous. When we want to know the previous, we have to search from the beginning of the list. A solution is to add one more list-node pointer into list node. (Read Section 4.8 of the text book.) An example typedef struct node *nodePointer; typedef struct node { int data; nodePointer llink; /* point to the left node */ nodePointer rlink; /* point to the right node */ }; Linked List

Doubly Linked List: An Example 12 data rlink llink 12 data rlink llink 20 30 Linked List

Doubly Linked List typedef struct node *nodePointer; typedef struct node { int data; nodePointer llink; /* point to the left node */ nodePointer rlink; /* point to the right node */ }; nodePointer createDL(int e){ nodePointer tmp; tmp = (nodePointer)malloc(sizeof(dlist)); tmp->data = e; tmp->llink = tmp; tmp->rlink = tmp; return tmp; } Suppose ptr points to any node in a doubly linked list. Then: ptr == ptr->llink->rlink == ptr->rlink->llink Linked List

Doubly Linked List: dinsert void dinsert(nodePointer node, nodePointer newonde) { /* insert the newnode into the right of the node*/ newnode->llink = node; newnode->rlink = node->rlink; node->rlink->llink = newnode; node->rlink = newnode; } 18 data rlink llink newnode node 12 data rlink llink 20 30 Linked List

Doubly Linked List: ddelete void ddelete(nodePointer node, nodePointer deleted){ if (node == deleted) printf("deletion of head is not permitted.\n"); else { deleted->llink->rlink = deleted->rlink; deleted->rlink->llink = deleted->llink; free(deleted); } node 12 data rlink llink 20 30 deleted Linked List

Lists v.s. Arrays Question-1: Do you know how many elements you need to handle? (max number? exact number? ...) Question-2: How do you use lists/arrays? Access elements (read/update values) Insert/delete elements For singly/doubly linked lists, we need to consider the insert/delete elements into/from a list at the cost of one more pointer. Linked List