Linked list C Programming
The size requirement need not be known at compile time. A linked list is a data structure that is used to model such a dynamic list of data items
ARRAY is sequential mapping, elements are fixed distance apart. makes insertion or deletion at any arbitrary position in an array a costly operation
Linked List not necessary that the elements be at a fixed distance apart an element is required to be linked with a previous element of the list done by storing the address of the next element
A linked list is a linear collection of data elements called nodes in which linear representation is given by links from one node to the next node.
Different from array, data elements of linked list are generally not lined in consecutive memory space; instead they are dispersed in various locations
Linked list element (node) is user defined structure data type, typically contains two parts
- Information/data field
- One/Two pointers, holding the address of next
Types of linked list:
- Singly linked list
- Circular linked list
- Doubly linked list
Singly Linked List Write a C Program to perform following operations on Singly Linked List ADT :
- Create ii. Insert iii. Delete iv. Display
# include <stdio.h> # include <conio.h> # include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *start = NULL;
int menu()
{
int ch; system("cls");
printf("\n 1.Create a list "); printf("\n ");
printf("\n 2.Insert a node at beginning "); printf("\n 3.Insert a node at end");
printf("\n ");
printf("\n 4.Delete a node from beginning"); printf("\n 5.Delete a node from Last");
printf("\n "); printf("\n 6.Displaying the list");
printf("\n ");
printf("\n 7.Exit ");
printf("\n\n Enter your choice: "); scanf("%d",&ch);
return ch;
}
NODE* getnode()
{
NODE * newnode;
newnode = (NODE *) malloc(sizeof(NODE)); printf("\n Enter data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL; return newnode;
}
void createlist(int n)
{
int i;
NODE *newnode, *temp;
for(i = 0; i < n; i++)
{
newnode = getnode(); if(start == NULL)
{
}
else
{
}
}
}
start = newnode;
temp = start;
while(temp -> next != NULL) temp = temp -> next;
temp -> next = newnode;
void display()
{
NODE *temp;
temp = start;
printf("\n The contents of List (Left to Right): \n"); if(start == NULL)
{
printf("\n Empty List"); return;
}
else
{
while(temp != NULL)
{
printf("%d-->", temp -> data); temp = temp -> next;
}
}
}
void insert_at_beg()
{
NODE *newnode; newnode = getnode(); if(start == NULL)
{
start = newnode;
}
else
{
newnode -> next = start; start = newnode;
}
}
void insert_at_end()
{
NODE *newnode, *temp;
newnode = getnode(); if(start == NULL)
{
start = newnode;
}
else
{
temp = start;
while(temp -> next != NULL) temp = temp -> next;
temp -> next = newnode;
}
}
void delete_at_beg()
{
NODE *temp;
if(start == NULL)
{
printf("\n No nodes are exist.."); return ;
}
else
{
temp = start;
start = temp -> next;
printf("\n Node deleted %d", temp->data); free(temp);
}
}
void delete_at_last()
{
NODE *temp, *prev;
if(start == NULL)
{
printf("\n Empty List.."); return ;
}
else
{
temp = start; prev = start;
while(temp -> next != NULL)
{
prev = temp;
temp = temp -> next;
}
prev -> next = NULL;
printf("\n Node deleted %d", temp->data); free(temp);
}
}
void main(void)
{
int ch, n;
while(1)
{
ch = menu(); switch(ch)
{
case 1:
if(start == NULL)
{
case 2:
}
else break;
printf("\n Number of nodes you want to create: "); scanf("%d", &n);
createlist(n);
printf("\n List created..");
printf("\n List is already created..");
case 3:
case 4:
case 5:
case 6:
case 7:
insert_at_beg(); break;
insert_at_end(); break;
delete_at_beg(); break;
delete_at_last(); break;
display(); break;
exit(0);
}
getch();
}
}