Вы находитесь на странице: 1из 35

List

is a Collection of nodes, where each node contains the address of next node. List are also called as linked list

Prof. Vishwanath, 9844340356

11

List
Each node in a list contains two parts

Data part :

holds actual data

Link part :

Holds address of next node.

List

data

data

data NULL

Prof. Vishwanath, 9844340356

22

List
Types of List

Based on storage type


1.

Linear list

2.

Non-Linear list

Based on type of data


1. 2.

Homogeneous list Non-Homogeneous List

Prof. Vishwanath, 9844340356

33

Advantages using array


1. Data accessing is faster
Any

nth element can be accessed directly


arr[10] reads 11th element directly

e.g:

2. Simple to implement

Prof. Vishwanath, 9844340356

44

Dis Advantages using array


1.

Can become Full as Size of the array is fixed Allocating "extra" space not possible Array items are stored contiguously. not enough contiguous space, will be problem. Can be wasteful, space may never be used

2.

3.

4. 5.

Insert and delete in middle of elements is tedious. Difficult to maintain sorted order Need to shift the elements
55

Prof. Vishwanath, 9844340356

Non-Homogeneous List
Example:

struct Listnode { int id;

char name[20];
int SEM; struct Listnode *next;

};
typedef Listnode Node;
6 6

Advantages of Linked lists


1. Are Dynamic

2. can be maintained in sorted order

without shifting elements.


3. Insert and delete at any position are

simple.

Prof. Vishwanath, 9844340356

77

Types of Linked Lists


1. Singly linked list 2. Circular singly linked list 3. Doubly linked list
4. Circular doubly linked list

Prof. Vishwanath, 9844340356

88

Types of Linked Lists


1. Singly linked list
Start

data

data

data

NULL

External pointer (start) Points to the first node

Last node contains Pointer to NULL


99

Prof. Vishwanath, 9844340356

Types of Linked Lists


2. Circular Singly linked list
Start

data

data

data

External pointer(start) Points to the Last node

Last node contain a pointer back to first node

Prof. Vishwanath, 9844340356

10 10

Types of Linked Lists


3. Doubly-linked list
NULL data right left data right left data NULL

Start

External pointer(start) Points to the first node


Each node has a forward and backwards pointer Can be Traversed forward or backward

Last node on both end are Null-terminated


11 11

Prof. Vishwanath, 9844340356

Types of Linked Lists


4. Circular doubly linked list

Start

data right

left data right

left data

Last node right-pointer points back to first node

First node left-pointer points to last node

Prof. Vishwanath, 9844340356

12 12

Singly Linked List

Declaration of node structure.


typedef struct { int data; struct node *next; } node;
Prof. Vishwanath, 9844340356

13 13

/* C-routine to allocate node */


Creating node.
node * getnode ( ) { node *p; p = (node *) malloc (sizeof (node)); if ( p == NULL) { printf (Not enough memory); exit (0); } return p;

Prof. Vishwanath, 9844340356

14

Operations on Singly Linked List Primitive operations:


1. Inserting a node into the list 2. Deleting a node from the list
3. Searching an item in the list 4. Traversing a list

Prof. Vishwanath, 9844340356

15 15

4. Traversing a list
Write code for Display Linked list

start
100

30

200 100

20

300

30

400
300

40

NULL 400

200

Prof. Vishwanath, 9844340356

16 16

void Display ( node *first ) { node *temp; if (first == NULL) { printf (Empty List ); return; } temp = first; while (temp != NULL) { printf ( %d , temp data); temp = temp next; } } /* end of Display */

Prof. Vishwanath, 9844340356

17

1. Insert a node
a) Insert at front

b) Insert at end c) Insert at given position

Prof. Vishwanath, 9844340356

18 18

void insert_front ( int item )

a)

Insert at front

node *temp;

temp = getnode ( );
tempdata = item ; tempnext = start; start = temp;

/* end of insert_front */

Prof. Vishwanath, 9844340356

19 19

b) Insert at end
start

200

last

400

20 300
200

30

400 300

40

NULL
400

30

NULL
500

Logic ..
1. Create new node 2. search last node
500
temp

3. last -> next = temp;


Prof. Vishwanath, 9844340356

20 20

void insert_end ( int item ) { node *temp, *last = start;


temp = getnode ( );
// create new node

temp -> data = item ; // add data if (start == NULL) { start = temp ; return ; } while ( lastnext != NULL ) //search last node
last = lastnext ; lastnext = temp; } /* end of insert_front */
Prof. Vishwanath, 9844340356

// create link

21

2. remove a node (delete)


a) remove at front

b) remove at end c) remove at given position

Prof. Vishwanath, 9844340356

22 22

a) remove at front
start 100 200

30
100

200

20

300

30

400 300

40

NULL
400

200

temp

Logic ..
1. Copy start to temp 2. Move start to next node

? 100

3. free (temp )
Prof. Vishwanath, 9844340356

23 23

void remove_front ( ) { node *temp; if (start == NULL) { printf ( ..List is Empty..); return start; }

temp = start;
start = start next;
// display temp

free (temp); } /* end of insert_front */

Prof. Vishwanath, 9844340356

24

b) remove at end
start prev last

200

400

? 500

20

300

30

400 300

400 40 NULL
400

50

NULL

200

500

Logic ..
1. No node in list No deletion 2. Only one node in list List becomes empty
Prof. Vishwanath, 9844340356

3. More nodes

Traverse last and prev


prev -> next = NULL; free (last);
25 25

void remove_end ( )

node *temp, *last, *prev;


if (start == NULL)
{ printf (..List is Empty..); return ; }

if ( start next == NULL) { free (start); start = NULL; }


last = start, prev = NULL;

return ;

while (lastnext != NULL) /* search last node */ { prev = last; last = lastnext ; } prevnext = NULL;

} /* end of insert_front */
Prof. Vishwanath, 9844340356

free (last);

26 26

c) Insert at given position


start

200

prev
300

20
200

300

30

600 400

40
400

500

30

NULL 500

300

pos = 3

30

400 ?
600

600
temp

1. Create new node

2. Mark prev of given pos


3. Copy next(prev) to next(temp) 4. Copy temp to next(prev)
Prof. Vishwanath, 9844340356

27 27

node insert_pos ( int pos, int item ) { node *temp, *prev ; int i; temp = getnode (item);
if ( pos==1 )

insert_front ( );
prev = start;

for ( i = 1; i < pos-1 ; i++ ) // Search position { if (prev == NULL) break; prev = prev -> next; } if ( prev == NULL ) { printf("\n***Invalid Position **\n"); free (temp); return ; } temp next = prev next ;
prev next = temp;

/* END OF FUNCTION */

Prof. Vishwanath, 9844340356

28 28

c) Remove at given position


start

200

prev
300

cur ? 400

20

300

30 400
300

40

500
400

50

NULL
500

200

pos = 3
1. No node in list No deletion 2. Deletion at 1st position Change start to next node
Prof. Vishwanath, 9844340356

3. More nodes

Traverse cur and prev


Copy next(cur) to next(prev) De-allocate cur
29 29

void remove_pos ( int pos )

node *cur , *prev ; int count ;


if (start == NULL) { printf("---Empt List----"); return ; } if (pos == 1) {

printf("\n***Item Deleted is %d **\n", start->data);

start = start->next ; free (cur); return ;

Prof. Vishwanath, 9844340356

30 30

prev = start; for (i = 1; i < pos-1 ; i++ ) { // Search prev Node for node to be deleted

}
{

if ( prev == NULL) break; prev = prev next;

if ( prev == NULL || prev next == NULL )


printf ( \n***Invalid Position **\n ); return ;
/* points to the node to be deleted */

cur = prev next; prev next = cur next;

printf ( "\n***Item Deleted is %d **\n", curdata);

free (cur);

Prof. Vishwanath, 9844340356

31 31

Stack implementation
Implementation :
Push() - Insert At Front
Pop() - Remove At Front

Prof. Vishwanath, 9844340356

32 32

Queue implementation
Insert At Rear Remove At Front

Prof. Vishwanath, 9844340356

33 33

Problems..
Write C-routine to concatenate two singly Linked lists..
List1
200

last
300

List2 ? 400

20
200

220

30

300
220

30

400 NULL
300

40

500
400

50

NULL 500

Function call:

List = concat (List1, List2)


Prof. Vishwanath, 9844340356

34 34

node * concat (node *List1, node * List2) { node *last = List1;


if ( List1 == NULL ) return List2;

Concatenate Two Lists


// List = List2

if ( List2 == NULL ) return List1;


while ( last ->next !=NULL ) last = last-> next;

// List = List1

// search last node from List-1

last->next = List2; return List1;

// connect Last node to the List2

Prof. Vishwanath, 9844340356

35 35

Вам также может понравиться