418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked...

43
ปปปปปป ปปปปปปป 418115: ปปปปปปปปปปปปปปป ปปปปปปปปป ปปปปปปปปปปปปปปป II

Transcript of 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked...

Page 1: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

ประมุ�ข ข�นเงิ�น

418115: การเข�ยนโปรแกรมุโครงิสร�างิโครงิสร�างิข�อมุ�ล II

Page 2: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked ListA linked list can be thought of a chain of

linked list elements.A linked list element contains a single data

item, and contains a pointer to the next linked list element.

It may also contain a pointer to the previous linked list element. In this case, we call it a doubly linked list element.

Page 3: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List (cont.)

11 42 7

128 398 4649

Page 4: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementationstruct _LLElement

{

int data;

_LLElement *next, *prev;

};

typedef struct _LLElement LLElement;

Page 5: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementaion (cont.)

LLElement *LLElement_new( int data ) used to create a new linked list element with

the given data.Usage:LLElement *element = LLElement_new(10);

Page 6: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)LLElement *LLElement_new( int data )

{

LLElement *result =

(LLElement *)malloc(sizeof(LLElement));

if (result != NULL)

{

result->data = data;

result->next = NULL;

result->prev = NULL;

}

return result;

}

Page 7: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementaion (cont.)

void LLElement_insert_after( LLElement *position, LLElement *e) inserts a linked list element “e” after “position”.

Page 8: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192this

e

Page 9: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192this

e

Page 10: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192this

e

Page 11: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192this

e

Page 12: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192this

e

Page 13: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192this

e

Page 14: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)void LLElement_insert_after(

LLElement *position, LLElement *e)

{

e->prev = position;

e->next = position->next;

if (position->next != NULL)

position->next->prev = e;

position->next = e;

}

Page 15: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementaion (cont.)

void LLElement_insert_before( LLElement *position, LLElement *e) inserts the linked list element “e” before

“position”.

Page 16: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192

position

e

Page 17: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192

e

position

Page 18: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192

e

position

Page 19: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192

e

position

Page 20: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192

e

position

Page 21: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

192

this

e

Page 22: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)void LLElement_insert_before(

LLElement *position, LLElement *e)

{

e->next = position;

e->prev = position->prev;

if (prev != NULL)

position->prev->next = e;

position->prev = e;

}

Page 23: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementaion (cont.)

void LLElement_remove(LLElement *e) removes the linked list element “e” from the

chain.In effect, it links e’s prev with next.

Page 24: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

this

Page 25: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

e

Page 26: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

e

Page 27: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

e

Page 28: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

e

Page 29: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)

11 42 7

128 398 4649

e

Page 30: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation (cont.)void LLElement_remove(LLElement *e)

{

if (e->prev != NULL)

e->prev->next = e->next;

if (e->next != NULL)

e->next->prev = e->prev;

e->prev = NULL;

e->next = NULL;

}

Page 31: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Element’s Efficiency?Space: O(1)Running Time

InsertBefore O(1)InsertAfter O(1)Remove O(1)

Page 32: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked ListTo simplify implementation, we will use two

“dummy” elements to act as the first element and the last element of the list.

These two dummies do not hold real data.Elements between them do.

Page 33: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List(cont.)

11 42 7

128 398 4649

???

head

???

tail

Page 34: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.)

typedef struct { LLElement *head, *tail; int size;} LinkedList;

size ค�อจำ�านวนสมุาชิ�กใน linked list

Page 35: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.) void LinkedList_init(LinkedList *list)

Initializes a linked list. Create the head element. Create the tail element. Link them together. Set the size to 0.

void LinkedList_init(LinkedList *list){ list->head = LLElement_new(0); list->tail = LLElement_new(0); list->head->next = tail; list->tail->prev = head; list->size = 0;}

0

head

0

tail

Page 36: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.)int LinkedList_get(LinkedList *list, int i)

{

int j;

LLElement *ptr = list->head->next;

for(j=0;j<i;j++)

ptr = ptr->next;

return ptr->data;

}

Page 37: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.)void LinkedList_set(LinkedList *list, int i, int x)

{

int j;

LLElement *ptr = list->head->next;

for(j=0;j<i;j++)

ptr = ptr->next;

ptr->data = x;

}

Page 38: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.)void LinkedList_find(LinkedList *list, int x){ int result = 0; LLElement *ptr = list->head->next; while (ptr != list->tail && ptr->data != x) { ptr = ptr->next; result++; } if (ptr == tail) return -1; else return result;}

Page 39: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.)void LinkedList_insert(LinkedList *list, int i, int x)

{

LLElement *ptr = list->head;

int j;

for(j=0;j<i-1;j++)

ptr = ptr->next;

LLElement_insert_after(ptr, LLElement_new(x));

list->size++;

}

Page 40: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Implementing List with Linked List (cont.)void LinkedList_remove(LinkedList *list, int i)

{

LLElement *ptr = head->next;

int j;

for(j=0;j<i;j++)

ptr = ptr->next;

LLElement_remove(ptr);

free(ptr);

list->size--;

}

Page 41: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation’s EfficiencySpace: O(n)Running Time:

Get(i) O(i) = O(n)Set(x,i) O(i) = O(n)Find(x) O(n)Insert(x,i) O(i) = O(n)Remove(x,i) O(i) = O(n)

Page 42: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation’s Efficiency (cont.)Notice that how you specify the operations

can have a lot of impact on the implementation’s efficiency.

We can insert a linked list element into a linked list in O(1) if you know the element just before or after it.

But, if we are given the position i to insert, it takes O(i) time just to get there.

Page 43: 418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.

Linked List Implementation’s Efficiency (cont.)How is linked list better than array?

The space is O(n) at all time.More efficient use of memory.

It performs some operation faster.Insert(x, 0)Remove(0)Both are O(1) in LinkedList, but O(n) in ArrayList.So, a queue implemented by a linked list takes O(1)

per operation.