Linked lists

Post on 23-Feb-2017

58 views 0 download

transcript

Linked Lists

2

Anatomy of a linked list A linked list consists of:

A sequence of nodes

a b c d

Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link The list may (or may not) have a header

myList

myList isn’t a header, it’s just a reference

3

More terminology

A node’s successor is the next node in the sequence The last node has no successor

A node’s predecessor is the previous node in the sequence The first node has no predecessor

A list’s length is the number of elements in it A list may be empty (contain no elements)

4

Creating links typedef struct link { int data; struct link *next; }myList; myList *head, *tail;

int main() { myList *p, *q;

p=(myList*)malloc(sizeof(myList)); q=(myList*)malloc(sizeof(myList));

p->data=44; q->data=97;

p->next=q; q->next=0; }

data next pointer

data next

data next p:q:

44

97

next

next p:q:

44 97

p: q:

5

Singly-linked lists Here is a singly-linked list (SLL):

Each node contains a value and a link to its successor (the last node has no successor)

We have a reference to the first node in the list The reference is null if the list is empty

a b c d

myList

6

Creating a simple list A head node is just an initial node that exists at the front of

every list, even when the list is empty The purpose is to keep the list from being null, and to point at

the first element A tail node is just an initial node that exists at the end of every

list, even when the list is empty. The purpose is to keep the list from being null, and to point at

the last element In the previous code, just add this in main: head=p tail=q

44 97

p: q:

head:

tail:

7

Traversing a SLL

The following method traverses a list (and prints its elements):

void traverse() { p=head; while(p!=0) { printf(“%d “, p->data); p=p->next; }

8

Traversing a SLL (animation)

489744

head

p

9

Inserting a node into a SLL There are many ways you might want to insert a new

node into a list: As the new first element As the new last element Before a given value After a given value

All are possible, but differ in difficulty

10

Inserting as a new last element This is probably the easiest method to implement void insertAtLast(int value) { p=(myList*)malloc(sizeof(myList)); p->data=value; p->next=tail->next; tail=p; } Use this as: insertAtLast(value);

11

Inserting at last (animation)

9744

head

48 p

Find the tail nodeFirst, copy the link from the node that’s already in the list

Then, change the link in the node that’s already in the list

tail

Then, change the link in the tail node

Example: Call: insertAtLast(48)

12

Inserting a node as a new first element

void insertAtFirst(int value) { p=(myList*)malloc(sizeof(myList)); p->data=value; p->next=head; head=p; } Use this as: insertAtFirst(value)

13

Inserting at first (animation)

9744

head

14 p

Find the head nodeFirst, change the link in the new node

tail

Then, change the link in the head node

48

Example: Call: insertAtFirst(14)

14

Inserting a node as a new first element void insertAtMiddle(int search_data, int value) { q=head; while(q!=0) { if(q->data==search_data) {

p=(myList*)malloc(sizeof(myList)); p->data=value; p->next=q->next; q->next=p; } p=p->next; }Use this as: insertAtMiddle(search_data, value)

Don’t change the sequence

15

Inserting after (animation)

489744

head

29p

Find the node you want to insert afterFirst, copy the link from the node that’s already in the list

Then, change the link in the node that’s already in the list

q

Example: Call: insertAtMiddle(29)

16

Deleting a node from a SLL In order to delete a node from a SLL, you have to

change the link in its predecessor This is slightly tricky, because you can’t follow a

pointer backwards Deleting the first node in a list is a special case, because

the node’s predecessor is the list header

17

Inserting as a new last element This is probably the easiest method to implement void delete(int value) { q=0; p=head; while(p!=0) { if(p->data==value) { if(p==head) head=p->next; else q->next=p->next; } q=p; p=p->next; }Use this as: delete(value);

18

Deleting an element from a SLL

489744

head

• To delete the first element, change the link in the header

• To delete some other element, change the link in its predecessor

• What will happen for last node??? Code &Visualize yourself

(predecessor)

974414

head

48

q p

p

19

Other operations on linked lists Most “algorithms” on linked lists—such as insertion,

deletion, and searching—are pretty obvious; you just need to be careful

Sorting a linked list is just messy, since you can’t directly access the nth element—you have to count your way through a lot of other elements

Question: How would you reverse a singly-linked list in place (that is, without creating any new nodes)?

20

The End