Linked List
Linked List
In linked list, each node contains a pointer to the next node in the list.
The last node in the list contains a null (or zero) pointer.
A circular list is identical to a chain except that the last node contains a
pointer to the first node.
A doubly linked list differs from the chain and the circular list in that
each node contains two pointers.
One points to the next node (as before), while the other points to the
previous node.
We can define structures with pointer fields that refer to the structure
type containing them
a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;
a b c
1 NULL 2 NULL 3 NULL
a b c
1 2 3
Number of 2 1
arguments
Syntax void *calloc void *malloc (size_in_bytes);
(number_of_blocks,
size_of_each_block_in_bytes);
Return value void pointer (void *). If the void pointer (void *). If the
allocation succeeds, a pointer allocation succeeds, a pointer to
to the block of memory is the block of memory is returned.
returned. If the allocation of If the allocation of memory fails, a
memory fails, a NULL pointer is NULL pointer is returned.
returned.
Ashish Patel @ SVMIT 15
Dynamic Memory Operators in C Example
struct node{
int data;
struct node *next;
};
struct node *ptr;
ptr
?
free(ptr);
?
ptr
int number;
struct node * link;
};
struct student
{
char name[20]; Name id grdPts next_student
int id;
double grdPts;
struct student *next_student;
}
1. Add a node.
2. Delete a node.
3. Search for a node.
4. Traverse (walk) the list. Useful for counting
operations or aggregate operations.
Deleting a node requires that we logically remove the node from the
list by changing various links and then physically deleting the node
from the list (i.e., return it to the heap).
Any node in the list can be deleted. Note that if the only node in the
list is to be deleted, an empty list will result. In this case the head
pointer will be set to NULL.
To physically delete a node:
First locate the node itself and its logical predecessor.
Change the predecessor’s link field to point to the deleted node’s
successor.
Free the memory using the free() function.
1. include<stdio.h> 9. main()
2. struct link_list 10. {
3. { 11. int ch;
4. int data; 12.
5. struct link_list *next; 13. top = (node*)malloc(sizeof(node));
6. }; 14. top->next = NULL;
7. typedef struct link_list node;
8. node *top;
top
16. while(1)
17. {
18. printf("\n1.push\n2.pop\n3.display\n4.exit\n");
19. printf("Enter your Choice:\n");
20. scanf("%d",&ch);
21. switch(ch)
22. {
23. case 1: push(); break;
24. case 2: pop(); break;
25. case 3: display(top); break;
26. case 4: exit();
27. }
28. }
29. }
top
Ashish Patel @ SVMIT 26
Example : C Program for Stack using Linked List - 5
DISPLAY Function
list = top
Ashish Patel @ SVMIT 27
Singly Linked List - Operations
#include<stdio.h> while(1)
#include<stdlib.h> {
printf("\n---------Menu---------\n");
struct linked_list printf(“Give Options");
{ printf("\nEnter choice:");
int data; scanf("%d",&ch);
struct linked_list *next; switch(ch)
}; {
typedef struct linked_list node; case 1: begin_insert(); break;
node *head; case 2: end_insert(); break;
void begin_insert(); case 3: insert_at_loc(); break;
void end_insert(); case 4: begin_delete(); break;
void insert_at_loc(); case 5: end_delete(); break;
void begin_delete(); case 6: del_at_loc(); break;
void end_delete(); case 7: search_item(); break;
void del_at_loc(); case 8: display(); break;
void search_item() case 9: exit(0); break;
void display(); default: printf("\nEnter valid choice ");
int main () { } //switch
int ch; } //while
} //main
void begin_insert()
{
node *ptr;
int no;
if(head == NULL)
{
printf("\nList is Empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nBegining node deleted");
}
}
A node in a doubly linked list differs from that in a singly linked list in
that it has two pointers
One points to the next node, while the other points to the previous
node
NULL Prev Data Next Prev Data Next Prev Data Next NULL
struct link_list
{
int info;
struct link_list *next;
struct link_list *prev;
};
head
3. Delete Next
4. Delete Previous
5. Display
6. Search
7. Replace
8. Insert at Location
9. Delete from Location
head
void del_next()
{
node *list;
while(head->next != NULL)
{
head = head->next;
}
list = head->prev;
printf("Deleted Element is %d\n",head->info);
free(head);
head = list; Prev NOdata next NULL
list->next = NULL;
display();
List
}
void del_prev()
{
node *list;
while(head->prev != NULL)
{
head = head->prev;
}
list = head->next;
printf("Deleted Element is %d\n",head->info);
free(head);
head = list; Prev NOdata next NULL
list->prev = NULL;
display();
List
}
The header node can be separated from the others by either heaving
a sentinel value as the info part or having a dedicated flag variable
to specify if the node is a header node or not
node *rear;
node *front;
13.switch(c)
Ashish Patel @ SVMIT 59
Insertion In Queue
void insert(node *rear)
{ else
node *temp; { while(rear->next!=NULL)
temp=(node *)malloc(sizeof(node)); {
rear=rear->next;
if(rear->next==NULL) }
{ printf("\n\nENTER THE VALUE: ");
printf("\n\nENTER THE VALUE: "); scanf("%d",&temp->info);
scanf("%d",&temp->info);
temp->next=NULL;
temp->next=NULL; rear->next=temp;
rear->next=temp; }
} }