You are on page 1of 22

Linked List

● ●

So far, you know two types of data structures, which are collections of data ● arrays ● stacks Linked lists are another collection type. Arrays, stacks and linked lists store "elements" on behalf of "client" code. The specific type of element is not important since essentially the same structure works to store elements of any type. ● Arrays / stacks of integers ● Arrays / stacks of doubles ● Arrays / stacks of customer accounts ● Arrays / stacks of ...
1

LInked List Structure

An array allocates memory for all its elements lumped together as one block of memory. A linked list allocates space for each element separately in its own block of memory called a "linked list element" or "node". All nodes of a list are connected together like the links in a chain. !ach node contains two fields" a "data" field to store whate#er element type the list holds for its client, and a "ne$t" field which is a pointer used to link one node to the ne$t node. !ach node is allocated in the heap with a call to malloc(), so the node memory continues to e$ist until it is e$plicitly deallocated with a call to free(). The front of the list is a pointer to the first node.

data = 1 next = 0xc124 head = 0xc224

data = 2 next = 0xc038

data = 3 next = NULL

Create List

The e$ample below will create the three&element list
next;
head = 0xc224 data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

struct node { int data; struct node !; "

#uild the list {1$ 2$ 3! in the hea% and store its head %ointer in a local stac& 'aria(le) *eturns the head %ointer to the caller) "

struct node #uild+ne,-o,hree () { struct node head = NULL; struct node second = NULL; struct node third = NULL; head = malloc (si.eof (struct node)); " allocate 3 nodes in the hea% second = malloc (si.eof (struct node)); third = malloc (si.eof (struct node)); head/0data = 1; " setu% first node " head/0next = second; second/0data = 2; " setu% second node " second/0next = third; third/0data = 3; " setu% third lin& " third/0next = NULL; " 1t this %oint$ the lin&ed list referenced (2 3head3 matches the list in the dra-in4) " return head; !

"

%

Count Elements in the List
● ●

(ass the list by passing the head pointer )terate o#er the list with a local pointer
data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

head = 0xc224
" 5i'en a lin&ed list head %ointer$ com%ute and return the num(er of nodes in the list) " int Len4th (struct node head) { struct node current = head; int count = 0; -hile (current 6= NULL) { count77; current = current/0next; ! return count; ! 'oid Len4th,est () { struct node m2List = #uild+ne,-o,hree (); int len = Len4th (m2List); " results in len == 3 !

"
'

Display Elements in the List
● ●

)terate o#er the list with the local pointer (rint the data contained in each node
data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

head = 0xc224
" 5i'en a lin&ed list head %ointer$ dis%la2 all num(ers stored in the list) "

'oid 8is%la2 (struct node head) { struct node current = head; -hile (current 6= NULL) { %rintf(39d 3$current/0data); current = current/0next; ! %rintf(3:n3); !
*

Add Element in Front of the List

(ass the pointer to the head to be able to modify it
data = = next = = ne-Node = 0xc24> data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

head;tr = 0xc224
'oid ;ush (struct node head;tr$ int data) { struct node ne-Node = malloc (si.eof (struct node)); ne-Node/0data = data; ne-Node/0next = head;tr; head;tr = ne-Node; ! 'oid ;ush,est () { struct node head = #uild+ne,-o,hree (); ;ush (<head$ 0); " note the < ;ush (<head$ 13); " head is no- the list {13$ 0$ 1$ 2$ 3! " !

"

+

Add Element in Front of the List

(ass the pointer to the head to be able to modify it
ne-Node = 0xc24> data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

data = 0 next = 0xc224

head;tr = 0xc224
'oid ;ush (struct node head;tr$ int data) { struct node ne-Node = malloc (si.eof (struct node)); ne-Node/0data = data; ne-Node/0next = head;tr; head;tr = ne-Node; ! 'oid ;ush,est () { struct node head = #uild+ne,-o,hree (); ;ush (<head$ 0); " note the < ;ush (<head$ 13); " head is no- the list {13$ 0$ 1$ 2$ 3! " !

"

,

Add Element in Front of the List

(ass the pointer to the head to be able to modify it
ne-Node = 0xc24> data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

data = 0 next = 0xc224

head;tr = 0xc24>
'oid ;ush (struct node head;tr$ int data) { struct node ne-Node = malloc (si.eof (struct node)); ne-Node/0data = data; ne-Node/0next = head;tr; head;tr = ne-Node; ! 'oid ;ush,est () { struct node head = #uild+ne,-o,hree (); ;ush (<head$ 0); " note the < ;ush (<head$ 13); " head is no- the list {13$ 0$ 1$ 2$ 3! " !

"

-

Free List

/ree each node starting from the beginning
data = 1 next = 0xc124 data = 2 next = 0xc038 data = 3 next = NULL

head = 0xc224 t = 0xc124

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

.

Free List

/ree each node starting from the beginning
data = 2 next = 0xc038 data = 3 next = NULL

head = 0xc224 t = 0xc124

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

10

Free List

/ree each node starting from the beginning
data = 2 next = 0xc038 data = 3 next = NULL

head = 0xc124 t = 0xc124

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

11

Free List

/ree each node starting from the beginning
data = 2 next = 0xc038 data = 3 next = NULL

head = 0xc124 t = 0xc038

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

1

Free List

/ree each node starting from the beginning
data = 3 next = NULL

head = 0xc124 t = 0xc038

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

1%

Free List

/ree each node starting from the beginning
data = 3 next = NULL

head = 0xc038 t = 0xc038

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

1'

Free List

/ree each node starting from the beginning
data = 3 next = NULL

head = 0xc038 t = NULL

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

1*

Free List

/ree each node starting from the beginning

head = 0xc038 t = NULL

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

1+

Free List

/ree each node starting from the beginning

head = NULL t = NULL

'oid ?reeList (struct node head) { -hile (head) { struct node t = head/0next; free (head); head = t; !; !

1,

Incorrect Implementation of Free List

1ersion below is shorter, but incorrect
● ●

)t attempts to access the freed memory area 2e cannot get rid of the temporary #ariable
head)

'oid ?reeList (struct node { -hile (head) { free (head); head=head/0next; !; !

1-

Append Node

/unction appends the node at the end of the list

'oid 1%%endNode (struct node head*ef$ int num) { struct node current = head*ef; struct node ne-Node; ne-Node = malloc (si.eof (struct node)); ne-Node/0data = num; ne-Node/0next = NULL; " s%ecial case for len4th 0 " if (current == NULL) { head*ef = ne-Node; ! else { " Locate the last node " -hile (current/0next 6= NULL) { current = current/0next; ! current/0next = ne-Node; ! !
1.

Copy List
● ●

/unction returns the pointer to the copy of the list 3ote there is no need for special case for an empty list

struct node @o%2List (struct node src) { struct node head = NULL; struct node dst=<head; -hile (src) { dst = malloc (si.eof (struct node)); ( dst)/0data = src/0data; ( dst)/0next = NULL; src = src/0next; dst = <(( dst)/0next); ! return head; !

0

Homework Pro lems

2rite a ;o%() function that is the in#erse of ;ush(). ;o%() takes a non&empty list, deletes the head node, and returns the head node4s data.
;o%,est() { struct node head = #uild+ne,-o,hree(); "" (uild {1$ 2$ 3! int a = ;o%(<head); " deletes 313 node and returns 1 " int ( = ;o%(<head); " deletes 323 node and returns 2 " int c = ;o%(<head); " deletes 333 node and returns 3 " int len = Len4th(head); " the list is no- em%t2$ so len == 0

● 'oid ● ●

! ●

"

2rite an iterati#e *e'erse() function that re#erses a list in place by rearranging all the )next pointers and the head pointer.

'oid *e'erse,est() { struct node head; head = #uild+ne,-o,hree(); ● *e'erse(<head); " head no- %oints to the list {3$ 2$ 1! ● ?reeList(head); !

"
1

Homework Pro lems

2rite the Aort() functions that sorts the list in place in ascending order. 5se the bubblesort algorithm.

'oid Aort,est() { struct node head = NULL; int i; for(i=0;iB10;i77) ;ush(<head$i) " head no- %oints to the list {C$ 8$ ))) $ 1$ 0! Aort(<head); " head no- %oints to the list {0$ 1$ 2$ ))) $ C! ?reeList(head); !

" "

2rite a complete set of functions 6including abo#e homework problems7 for the list of 8ero&terminated strings. List should store the copies of strings, so do not forget about the proper memory management.