Download presentation

Presentation is loading. Please wait.

Published byKarissa Gammell Modified over 2 years ago

1
CSCI2100B Linked List Jeffrey

2
Linked List4-2 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? insert 30

3
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 List4-3

4
Linked List4-4 A Singly Linked List Example What is the advantage/disadvantage of the singly linked representation in terms of read/update values and insertion/deletion of nodes? 12 datalink datalink listPointer l NULL 30 datalink

5
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();... } Linked List4-5 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.

6
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. The Length and the nth Position listPointer l NULL Linked List4-6

7
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 NULL Linked List4-7

8
Linked List4-8 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; } listPointer l NULL prev next

9
Linked List4-9 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 NULL listPointer l e

10
Linked List4-10 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 NULL end e NULL

11
Linked List4-11 Singly Linked List: Delete listPointer l NULL 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)

12
Linked List4-12 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 NULL

13
Another Set of Operations In the textbook (Section 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 List4-13

14
Linked List4-14 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);}

15
Linked List4-15 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"); } Dynamically Linked Stack

16
Linked List4-16 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); }

17
Linked List4-17 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"); }

18
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 List4-18

19
Linked List4-19 Doubly Linked List: An Example 12 datarlinkllink 12 datarlinkllink 20 datarlinkllink 30 datarlinkllink

20
Linked List4-20 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

21
Linked List4-21 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 datarlinkllink newnode node 12 datarlinkllink 20 datarlinkllink 30 datarlinkllink

22
Linked List4-22 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); } deleted node 12 datarlinkllink 20 datarlinkllink 30 datarlinkllink

23
Linked List4-23 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.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google