Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 261 - Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.

Similar presentations


Presentation on theme: "CS 261 - Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated."— Presentation transcript:

1 CS 261 - Fall 2009 Linked List Introduction

2 Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated and released as necessary. Each link points to next link in sequence, sometimes to previous link. Lots of variations on a simple idea

3 A typical Link Structure struct link { EleType value; struct link * next; };

4 Some variations in Linked lists List have header (special value to point to start) Use null as terminator, or special value for end Use single or double links? Pointer to first element, or pointer to first and last

5 Simplest Example, List Stack List stack is the simplest data structure that can be implemented using linked list idea Keep pointer to first element (null if empty) Elements are added or removed from front can only access first element

6 Picture of list stack

7 Code for list stack struct ListStack { struct link * firstLink; /* initialize routine sets to zero */ }; struct link * _newLink (EleType v, link * n) { /* used internally */ struct link * newLink = (struct link *) malloc(sizeof(struct link)); assert(newLink != 0); newLink->next = n; newLink->value = v; return newLink; } void listStackPush (struct listStack *stk, EleType val) { stk->firstLink = _newLink(val, stk->firstLink); }

8 Other Operations How do you return the top element in stack? How do you remove the top element? How do you tell if the stack is empty? You will do all of these in the worksheet

9 How fast is List Stack? Compare to dyArrayStack push - list O(1) always, dyArray(1) expected pop - list O(1) always, dyArray same top - list O(1) always, dyArray same In practice dyArray is slightly faster in real timinings.

10 But what about queues? An array queue is hard to do, because you can't add to the beginning without sliding things up. With lists it is easy. Just keep a pointer to both the front AND the back. Elements added to the back, removed from front

11 Picture of list queue

12 Class Structure for List Queue struct listQueue { struct link * firstLink; struct link * lastLink; }; void listQueueAddBack (struct listQueue *lst, EleType val); void listQueueAddFront (struct listQueue * lst, EleType val);

13 Elements are Added to end void listQueueAddLast (struct listQueue *lst, EleType val) { struct link * lnk = _newLink(val, 0); if (lst->lastLink != 0) lst->lastLink->next = lnk; lst->lastLink = lnk; if (firstLink == 0) lst->firstLink = lnk; }

14 Empty test - just see if link is null Int listQueueIsEmpty (struct listQueue *lst) { return lst->firstLink == 0; }

15 Elements Removed from Front void listQueueRemoveFirst (struct listQueue *lst) { struct link * lnk = lst->firstLink; assert (! listQueueIsEmpty(lst)); lst->firstLink = lnk->next; free(lnk); if (lst->firstLink == 0) /* ie, removing last*/ lst->lastLink = 0; }

16 What about a deque? What if we want to add and remove elements from both front and back? Need to use links going both forward and backwards Makes adding a new link harder, as must maintain both forward and backward links. Will do that in tomarrows lesson

17 What about a Bag? Contains is easy - just a loop Add is easy - can either add to front or to back Remove is the tricky one. How to patch up links after removing an element. Two solutions, double links or previous pointers (will do double links tomarrow)

18 Previous Pointers As you loop over the links looking for value to remove, keep pointer to previous element struct link * prevLink = null; struct link * current = lst->firstLink; for ( ; current != null; current = current->link) { if (EQ(current->value, testValue)) { // do what needs to be done return; } prevLink = current; }

19 Your turn See if you can do stack (should be trivial) See if you can do queue (only slightly harder) If time, see if you can do bag (requires a little more thought)


Download ppt "CS 261 - Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated."

Similar presentations


Ads by Google