0% found this document useful (0 votes)
22 views79 pages

Module 3-ND

The document discusses linked lists and their implementation. It contains the following key points: 1. Linked lists are linear data structures where each node contains a data field and a pointer to the next node. This allows dynamic memory allocation and easy insertion/deletion. 2. The document describes creating nodes, different types of linked lists, traversing, inserting, deleting and implementing stacks/queues using linked lists. 3. Circular linked lists are also covered, where the last node points back to the first node, allowing elements to be accessed from any given node. Operations like insertion and deletion are simpler in circular lists.

Uploaded by

Neetha Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views79 pages

Module 3-ND

The document discusses linked lists and their implementation. It contains the following key points: 1. Linked lists are linear data structures where each node contains a data field and a pointer to the next node. This allows dynamic memory allocation and easy insertion/deletion. 2. The document describes creating nodes, different types of linked lists, traversing, inserting, deleting and implementing stacks/queues using linked lists. 3. Circular linked lists are also covered, where the last node points back to the first node, allowing elements to be accessed from any given node. Operations like insertion and deletion are simpler in circular lists.

Uploaded by

Neetha Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

LINKED LIST

MODULE 3

1
Linked List

Linked list is linear collection of data elements called


nodes with each node having 2 parts
INFORMATION of element
ADDRESS of next node in the list

Example
External ptr

bat  cat 3000 sat 4000 vat NULL

Info
first
2
link
Representation of linked list in memory
External ptr

X  T  A  K NULL
INFO LINK
1

2 A 5

3
START 7
4 T 2

5 K 0

7 X 4

3
8
creating a node with value
Representation of node in linked list
struct list {
int info;
struct list *link;
};
Typedef struct list *NODE;

Creating a node 10 NUL


L
NODE getnode(NODE first)
{
first=(NODE) malloc (sizeof(NODE));
if(first==NULL)
{ printf(out of memory) exit;}
first->info=10;
first->link=NULL;
Return first;
}
Types of Lined List
Singly Linked List
Doubly Lined List
Circular Singly Lined List
Circular Doubly Lined List
Lined List with Header Node

5
Advantages of Linked list
Dynamic Memory Allocation
Insertion and Deletions are Easy
Traverse in both directions in Doubly Lined List
No waste of memory
Stack and Queue can be easily implemented

Disadvantages of Linked List


• More memory required to maintain nodes
• Traversing is difficult

6
Traversing a linked list
Algorithm: Traversing(LIST)
This algorithm traverses LIST. The variable PTR
points to the node currently being processed
Step 1 : Set PTR:=first
Step 2: Repeat step 3 and 4 while PTR != NULL
Step3: print PTR->info
Step 4 : Set PTR=PTR->link
[End of Step2 loop]
Step 5 :exit

7
PTR
Inserting an item into Linked List
Insert at Starting position/Front end
Insert at rear end/last position
Insert at specified position

8
Insert at starting/front position
NODE insert_front(int item,NODE head)
{ NODE temp;
 temp=(NODE)malloc(sizeof(NODE));
 if(temp==NULL){printf(no memery) exit;}
 temp->info=item
 temp->link=first;
 return temp;
} 12
null
13 33 null
12

temp
9
Insert at last/rear position

 NODE insert_rear()
 { NODE temp,cur;
 temp=(NODE)malloc(sizeof(NODE));
 if(temp==NULL){printf(“no memery”) exit;}
 temp->info=12
 temp->link=NULL;
 if (first==NULL) return temp;
 cur=first;
 while(cur->link!=NULL)
 { cur=cur->link; }
 cur->link =temp;
 return first; 13 33 null 12 null
 } 12
null
cur
first temp
10
Insert at specified position

NODE insert_pos()
{ NODE temp;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=4
temp->link=NULL;
if (first==NULL && pos==1) return temp;
if(first==NULL){ printf(invalid position) return
first; }
13 33 null 12 null
if(pos==1) {temp->link=first; return temp; }
null cur
first
11
Cont..
count=1; cur=first; prev=NULL;
while(cur!=NULL && count!=pos)
{ prev=cur;
cur=cur->link; count++;
}
if(count=pos)
{ prev->link=temp;
temp->link=cur
return first;
} printf(“invalid pos”); return first;
1p(“in
12 pvppPpPposition2
Deleting an item from Linked List
Deleting a node from Staring position/Front end
Delete from rear end/last position
Delete from specified position

13
Delete from front end
NODE delete_front(NODE first)
{ if(first==NULL)
 { printf(“list is empty”); return NULL; }
 temp=first;
 temp=temp->link;
 printf(“deleted item = %d”,first->info);
 free(first);
 return temp;
}
 13
14
Delete a node at rear end
NODE delete_rear(NODE first)
{ if(first==NULL)
 { printf(“list is empty”); return NULL; }
 if(first->link=NULL)
 { printf(“item deleted =%d “,first->info)
 free(first);
 return NULL;
 }

15
Cont..
 Prev=NULL;
 Cur=first;
 While(cur->linl!=NULL)
{prev=cur;
cur=cur->link;
}
printf(“item deleted =%d “,cur->info)
free(cur);
prev->link=NULL;
return first ;
}
16
Delete from specified position
NODE delete_pos(NODE first)
{ if(first==NULL)
 { printf(“list is empty”); return NULL; }
 if(first->link=NULL&& pos=1)
 { printf(“item deleted =%d “,first->info)
 free(first);
 return NULL;
 }
 if(first->link=NULL&& pos!=1)
 { printf(“invalid position”) return ; }
17
count=1; cur=first; prev=NULL;
while(cur!=NULL && count!=pos)
{ prev=cur;
cur=cur->link; count++;
}
if(count=pos)
{ prev->link=cur->link;
printf(“item deleted =%d “,cur->info)
free(cur);
return first;
} printf(“invalid pos”); return first;
1p(“in

18
Implementing stack using Linked list
Insert_front()
Delete_front()
Display()

OR
Insert_rear()
Delete_reat()
Display()

19
Implementing queue using Linked list
Insert_front()
Delete_rear()
Display()

OR
Insert_rear()
Delete_front()
Display()

20
Implementing double ended queue using
Linked list
Insert_front()
Insert_rear()
Delete_front()
Delete_rear()
Display()

21
Circular Singly Linked List

1000

Advantages:
 Every node can be accessed from the given node
 deletion is easy
 Circular list is very useful in case of Game play,to
give turns for each player without any failure

22
Insert at front position to circular linked list
 NODE insert_front()
 { NODE temp;
 temp=(NODE)malloc(sizeof(NODE));
 if(temp==NULL){printf(no memery) exit;}
 temp->info=12
 if(last==NULL)
 last=temp
 else temp->link=last->link
 last->link=temp;
 return last;
} 12
null

23
Insert at rear position to circular linked list
 NODE insert_front()
 { NODE temp;
 temp=(NODE)malloc(sizeof(NODE));
 if(temp==NULL){printf(no memery) exit;}
 temp->info=12
 if(last==NULL)
 last=temp; return last;
 else { temp->link=last->link
 last->link=temp;
 return temp; }

} 12
null

24
Delete at front of circular linked list
{ NODE first;
 if(last=NULL)
 { printf(“list is empty”) return NULL;
 }
 if(last->link=last)
 { printf(“the deleted element is %d“, last->info)
 free(last); return NULL;
 }
 first=last->link
 last->link=first->link;
 printf(“the deleted element is %d“, first->info)
 free(first); return last;
 }
25
Delete at rear of circular linked list
{ NODE first;
if(last=NULL)
{ printf(“list is empty”) return NULL;
}
if(last->link=last)
{ printf(“the deleted element is %d“, last->info)
free(last); return NULL;
}
prev=last->link
while(prev->linl!=last) { prev=prev->link }
prev->link=last->link;
printf(“the deleted element is %d“, first->info)
free(last); return prev;
}
26
Display
 Void display()
{ NODE first;
if(last=NULL)
{ printf(“list is empty”) return NULL;
}
 first=last->link
 while(first!=last)
 { printf(“%d”,first->info);
 first=first->link;
 } printf(“%d”,first->info);
 }

27
Circular List with Header node-Insert at
front position
NODE insert_front()
{ NODE temp,first;
 temp=(NODE)malloc(sizeof(NODE));
 if(temp==NULL){printf(no memery) exit;}
 temp->info=12
 first=head->link
 head->link=temp
 temp->link=first
 return head
} 12
null

28
Circular List with Header node-Insert at
rear position
 NODE insert_front()
 { NODE temp;
 temp=(NODE)malloc(sizeof(NODE));
 if(temp==NULL){printf(no memery) exit;}
 temp->info=9
 cur=head->link
 while(cur->link!=head)
 { cur=cur->link }
 cur->link=temp
 temp->link=head
 return head
} 12
null

29
Delete at front of circular linked list
{ NODE first,second;
 if(head->link=head)
 { printf(“list is empty”) return head;
 }
 first=head->link;
 second=first->link;
 head->link=second;
 printf(“the deleted item is”, first-info)
 free(first);
 return head
}
30
Delete at rear of circular linked list
{ NODE prev,cur;
 if(head->link=head)
 { printf(“list is empty”) return head;
 }
 cur=head->link;
 prev=head
 while(cur->link!=head)
 { prev=cur;
 cur=cur->link;
 }
 prev->link=head
 printf(“the deleted item is”, cur->info)
 free(cur);
 return head
}

31
Display
 Void display()
{ NODE first;
if(head->link=head)
{ printf(“list is empty”) return head;
}
 first=head->link
 while(first!=head)
 { printf(“%d”,first->info);
 first=first->link;
 }
}

32
Different operations on Singly Linked List
To Find length of Linked List
int length( NODE first)
{ NODE cur;
int count=0;
if(first==NULL)return 0;
cur=first
while(cur!=NULL)
{ count++; cur=cur->link; }
return count;
}
33
Search for an item
void search( int key, NODE first)
{ NODE cur;
if(first==NULL)return ;
cur=first
while(cur!=NULL)
{ if(key=cur->info) break;
cur=cur->link; }
if(cur==NULL)
{ printf(“SEARCH IS UNSUCESSFUL”);
return;
}
printf(“SEARCH IS SUCESSFUL”);
}
34
Concatenate two Lists
NODE concat( NODE first, NODE sec)
{ NODE cur;
if(first==NULL)return sec;
if(sec=NULL)return first;
cur=first;
while(cur->link!=NULL)
{ cur=cur->link;
}
cur->link=sec;
return first;
35 }
Reverse the list

36
Reverse the list
NODE reverse( NODE first)
{ NODE cur=NULL,temp;
while(first!=NULL)
{ temp=first->link;
first->link=cur;
cur=first;
first=temp;
}
return cur;
}
37
Creating an Ordered Linked List
NODE insert(int item, NODE first)
{ NODE temp,prev,cur;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL)
{printf(no memery) exit;}
temp->info=item;
temp->link=NULL;
if(first=NULL)return temp;
if(item<=first->info)
{ temp->link=first;
return temp;
}

38
Cont..
Prev= NULL;
Cur=first;
While(cur!=NULL && item>cur->info)
{prev=cur;
cur=cur->link;
}
prev->link=temp;
temp->link=cur;
return first;
}
39
Delete a node whose info field is specified
NODE delete(int item, NODE first)
{ NODE prev,cur;
if(first==NULL)
{printf(list is empty) return NULL;}
if(item=first->info)
{cur=first;
first=first->link;
free(cur);
return(first);
}
40
Cont..
Prev=NULL
Cur=first;
While(cur!=NULL)
{if(item=cur->info)break;
Prev=cur;
Cur=cur->link;}
If(cur=NULL)
{ printf(“unsuccessful search”) return first;}
Prev->link=cur->link;
Free(cur);
Return(first);
}
41
Singly Linked List with Header
Node

42
Insert at Front end of list with header node
NODE insert_front(int item,NODE head)
{ NODE temp,first;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item
first=head->link; temp->link=head->link
head->link=temp
head->link=temp
temp->link=first;
return head;
} 12
null

43
Insert at rear end of LL with header node

NODE insert_rear(int item, NODE head)


{ NODE temp,cur;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(“no memery”) exit;}
temp->info=item
temp->link=NULL;
cur=head->link
while(cur->link!=NULL)
{ cur=cur->link; }
cur->link =temp;
return head;
} 12
44 null
Delete from front end of LL with header
node
NODE delete_front(NODE head)
{ NODE first, cur;
{ if(head->link==NULL)
{ printf(“list is empty”); return head; }
first=head->link;
cur=First->link
head->link=cur;
printf(“deleted item = %d”,first->info);
free(first);
return head;
}
13

45
Delete a node at rear end of LL with header
node.
NODE delete_rear(NODE head)
{ NODE prev,cur
if(head->link==NULL)
{ printf(“list is empty”); return head; }
prev=head; cur=head->link;
While(cur->link!=NULL)
{ prev=cur; cur=cur->link;}
prev->link=NULL;
printf(“item deleted =%d “,cur->info)
free(cur);
return head;
}

46

Doubly Linked List

47
Insert at front position
NODE insert_front(int item,NODE first)
{ NODE temp;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item
temp->llink=temp->rlink=NULL
if(first==NULL) return temp;
temp->rlink=first;
first->llink=temp;
return temp;
48 } 12
null
Insert at rear end
NODE insert_rear(int item,NODE first)
{ NODE temp,cur;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item;
temp->llink=temp->rlink=NULL
if(first==NULL) return temp;
cur=first;
while (cur->rlink!=NULL)
{cur=cur->rlink;}
cur->rlink=temp;
temp->llink->cur;
return first;
49 } 12
null
Delete from front end
NODE delete_front(NODE first)
{ NODE cur;
if(first==NULL)
{ printf(“list is empty con’t delete”);
return NULL;
}
If(first->rlink=NULL)
{ printf(‘the deleted item=%d\n”,first->info);
free(first);
return NULL;
}
50
cur=first->rlink;
cur->llink=NULL;
printf(“item deleted =%d”,first->info);
free(first);
return cur;
}

51
Delete from rear end
NODE delete_front(NODE first)
{ NODE cur;
if(first==NULL)
{ printf(“list is empty con’t delete”);
return NULL;
}
If(first->rlink=NULL)
{ printf(‘the deleted item=%d\n”,first->info);
free(first);
return NULL;
}
52
Cont..
Prev=NULL;
Cur=first;
While(cur->rlink!=NULL)
{ prev=cur;
cur=cur->rlink;
}
printf(“item deleted =%d”,cur->info);
free(cur);
Prev->rlink=NULL;
return First;
}
53
Display
Void display(NODE first)
{ NODE cur; int count=0;
if(first==NULL)
{ printf(“list is empty”);
return;
}
Printf(“content of list are”);
cur=first;
while(cur!=NULL)
{ PRINTF(“%d”,cur->info);
count++;
cur=cur->rlink;
}
printf(“num of nodes %d”,count);
54 }
Dequeue using Doubly Linked List
Insert at Front
Insert at Rear
Delete from front end
Delete from rear end
Display

55
Insert at front end of circular doubly LL
NODE insert_front(int item,NODE first)
{ NODE temp,last;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item
temp->llink=temp->rlink=temp;
if(first==NULL) return temp;
last=first->llink;
temp->rlink=first;
first->llink=temp;
last->rlink=temp;
56
temp->llink=last;
return temp;
Insert at rear end
NODE insert_rear(int item,NODE first)
{ NODE temp,last;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item;
temp->llink=temp->rlink=temp;
if(first==NULL) return temp;
last=first->llink;
last->rlink=temp;
temp->llink=last;
first->llink=temp;
temp->rlink=first;
return first;
} 12
null
57
Delete from front end
NODE delete_front(NODE first)
{ NODE sec,last;
if(first==NULL)
{ printf(“list is empty con’t delete”);
return NULL;
}
If(first->rlink=first)
{ printf(‘the deleted item=%d\n”,first->info);
free(first);
return NULL;
}
58
sec=first->rlink;
Last=first->llink;
Sec->llink=last;
Last->rlink=sec;
printf(“item deleted =%d”,first->info);
free(first);
return sec;
}

59
Delete from rear end
NODE delete_front(NODE first)
{ NODE last,prev;
if(first==NULL)
{ printf(“list is empty con’t delete”);
return NULL;
}
If(first->rlink=first)
{ printf(‘the deleted item=%d\n”,first->info);
free(first);
return NULL;
}
60
Last=first-?llink;
Prev=last->llink;
Prev->rlink=first;
First->llink=prev
Printf(deleted item %d”,last->info);
Free(last);
Return first;
}

61
Display
Void display(NODE head)
{ NODE cur,last;
if(first==NULL)
{ printf(“list is empty”);
return;
}
Printf(“content of list are”);
cur=first;
last=first->llink;
while(cur!=last)
{ PRINTF(“%d”,cur->info);
cur=cur->rlink;
}
printf(“%d”,cur->info);
62 }
Circular doubly linked list with header node

63
Insert at front end of circular doubly LL
with header node
NODE insert_front(int item,NODE head)
{ NODE temp,first;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item;
first=head->rlink;
temp->llink=head;
head->rlink=temp
temp->rlink=first;
first->llink=temp;
64 return head;
Insert at rear end on Circular DLL with
header node
NODE insert_rear(int item,NODE head)
{ NODE temp,last;
temp=(NODE)malloc(sizeof(NODE));
if(temp==NULL){printf(no memery) exit;}
temp->info=item;
last=head->llink;
last->rlink=temp;
temp->llink=last;
temp->rlink=head;
head->llink=temp;
return head;
} 12
null
65
Delete from front end of Circular DLL with
header node
NODE delete_front(NODE head)
{ NODE sec,first;
if(head->rlink==head)
{ printf(“list is empty con’t delete”);
return head;
}
first=head->rlink;
sec=first->rlink;
head->rlink=sec;
sec->llink=head;
printf(“deleted item %d”,first->info);
free(first);
return head;
66 }
Delete from rear end of Circular DLL with
header node
NODE delete_rear(NODE head)
{ NODE last,prev;
if(head->rlink==head)
{ printf(“list is empty con’t delete”);
return head;
}
last=head->llink;
prev=last->llink;
head->llink=prev;
prev->rlink=head;
printf(“deleted item %d”,last->info);
free(last);
return head;
67 }
Display Circular DLL with header node
Void display(NODE first)
{ NODE cur;
if(head->rlink==head)
{ printf(“list is empty”);
return head;
}
Printf(“content of list are”);
cur=head->rlink;
while(cur!=head)
{ PRINTF(“%d”,cur->info);
cur=cur->rlink;
}

68
}
69
A[10]

70
Applications of Linked List
Stack ,Queue , hash table and binary tree can be
implemented using DLL.
The cache in your browser that allows you to hit the
back button(Linked list of URLs)
Undo function in word or photoshop
Evaluation and Addition of polynomials
Addition of long integers
Sparse Matrix
Symbol table implementation

71
72
Polynomials
Representation

struct polynode {
int coef;
int expon;
struct polynode * link;
};
typedef polynode * poly_pointer;
poly_pointer a, b, c;

coef expon link

73
Example
a= 3x14+ 2x8+ 1
b= 8x14- 3x10+ 10x6

3 14 . 2 8 . 1 0 null
b

8 14 . -3 10 . 10 6 null

74
Adding Polynomials

3 14 2 8 1 0 NULL
a
8 14 -3 10 10 6 NULL
b
11 14 NULL a->expon == b->expon
d
3 14 2 8 1 0 NULL
a
8 14 -3 10 10 6 NULL
b
11 14 -3 10 NULL a->expon < b->expon
d
75
Adding Polynomials (Continued)

3 14 2 8 1 0 NULL
a
8 14 -3 10 10 6 NULL
b
11 14 -3 10 2 8 NULL
d
a->expon > b->expon

76
Algorithm for Adding Polynomials
poly_pointer padd(poly_pointer a, poly_pointer b)
{
poly_pointer rear, temp;
int sum;
rear =(poly_pointer)malloc(sizeof(poly_node));
if (IS_FULL(rear)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
while (a && b) {
switch (COMPARE(a->expon, b->expon)) {

77
case -1: /* a->expon < b->expon */
rear=attach(b->coef, b->expon, rear);
b= b->link;
break;
case 0: /* a->expon == b->expon */
sum = a->coef + b->coef;
if (sum)
rear=attach(sum,a->expon,rear);
a = a->link; b = b->link;
break;
case 1: /* a->expon > b->expon */
rear=attach(a->coef, a->expon, rear);
a = a->link;
}
}
for (; a; a = a->link)
attach(a->coef, a->expon, rear);
for (; b; b=b->link)
attach(b->coef, b->expon, rear);

return rear;
}

78
 polypointer attach(float coef, int expon ,polypointer header)
{ polypointer temp=malloc(temp,sizeof(*temp);
ptr=rear;
temp->coef=coef;
temp-> expon =expon;
temp->link=NULL;
while(ptr->link!=NULL)
ptr=ptr->link;
(ptr)->link=tem
return header;
}
79

You might also like