PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct node
{
struct node *pre;
int data;
struct node *next;
};
void insert_node_at_begining(struct node**head,int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("Memory allocation faild!!\n");
return;
}
newnode->pre=NULL;
newnode->data=data;
newnode->next=*head;
if(*head!=NULL)
{
(*head)->pre=newnode;
}
*head=newnode;
}
void insert_node_at_end(struct node**head,int data)
{
struct node*newnode=(struct node*)malloc(sizeof(struct node));
struct node*temp=*head;
if(newnode==NULL)
{
printf("Memory allocation failed!!\n");
return;
}
newnode->data=data;
newnode->pre=NULL;
newnode->next=NULL;
if(*head==NULL)
{
*head=newnode;
return;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->pre=temp;
temp->next=newnode;
}
void insert_node_at_specific_position(struct node**head,int data,int pos)
{
struct node*newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("Memory allocation failed!!\n");
return;
}
newnode->pre=NULL;
newnode->data=data;
newnode->next=NULL;
if(pos<=0)
{
printf("invalid position!\n");
free(newnode);
return;
}
if(pos==1)
{
newnode->next=*head;
if(*head!=NULL)
(*head)->pre=newnode;
*head=newnode;
return;
}
struct node*temp=*head;
for(int i=1;i<pos-1&&temp!=NULL;i++)
{
temp=temp->next;
}
if(temp==NULL)
{
printf("position %d is out of range:\n",pos);
free(newnode);
return;
}
newnode->next=temp->next;
newnode->pre=temp;
if (temp->next != NULL)
temp->next->pre=newnode;
temp->next = newnode;
}
void delete_node_at_first(struct node**head)
{
if(*head==NULL)
{
printf("List is empty:\n");
return;
}
struct node*temp=*head;
printf("%d is deleted at first:\n",temp->data);
*head=(*head)->next;
if(*head!=NULL)
{
(*head)->pre=NULL;
}
temp->next=NULL;
free(temp);
}
void delete_node_at_end(struct node **head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *temp = *head;
// If only one node is present
if (temp->next == NULL) {
printf("%d is deleted at the end.\n", temp->data);
free(temp);
*head = NULL;
return;
}
// Traverse to the last node
while (temp->next != NULL) {
temp = temp->next;
}
printf("%d is deleted at the end.\n", temp->data);
temp->pre->next = NULL;
free(temp);
}
void delete_node_at_position(struct node **head, int pos) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
if (pos <= 0) {
printf("Invalid position.\n");
return;
}
struct node *temp = *head;
// Delete the first node
if (pos == 1) {
printf("%d is deleted from position %d.\n", temp->data, pos);
*head = temp->next;
if (*head != NULL) {
(*head)->pre = NULL;
}
free(temp);
return;
}
// Traverse to the node at (pos - 1)
for (int i = 1; i < pos && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position %d is out of range.\n", pos);
return;
}
printf("%d is deleted from position %d.\n", temp->data, pos);
if (temp->pre != NULL) {
temp->pre->next = temp->next;
}
if (temp->next != NULL) {
temp->next->pre = temp->pre;
}
free(temp);
}
void display(struct node**head)
{
if(*head==NULL)
{
printf("List is empty:\n");
return;
}
struct node*temp=*head;
while(temp!=NULL)
{
printf("%d<->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
int main()
{
int data,choice,pos;
struct node*head=NULL;
do
{
printf("============MENU============\n");
printf("1).INSERT NODE AT BEGGINING:\n");
printf("2).INSERT NODE AT END\n");
printf("3).INSERT NODE AT SPECIFIC:\n");
printf("4).DELETE NODE AT BEGINING:\n");
printf("5).DELETE NODE AT END:\n");
printf("6).DELETE NODE AT SPECIFIC:\n");
printf("7).DISPLAY THE LIST:\n");
printf("8).EXIT PROGRAM:\n");
printf("=============================\n");
printf("Enter choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Data You Want To add at Begining:\n");
scanf("%d",&data);
insert_node_at_begining(&head,data);
break;
case 2:
printf("Enter data you want to add at end:\n");
scanf("%d",&data);
insert_node_at_end(&head,data);
break;
case 3:
printf("Enter position you want to insert node:\n");
scanf("%d",&pos);
printf("Enter data you want add:\n");
scanf("%d",&data);
insert_node_at_specific_position(&head,data,pos);
break;
case 4:
delete_node_at_first(&head);
break;
case 5:
delete_node_at_end(&head);
break;
case 6:
printf("Enter position you want to delete:\n");
scanf("%d",&pos);
delete_node_at_position(&head,pos);
break;
case 7:
display(&head);
break;
case 8:
printf("Wait 3 seconds:\n");
sleep(3);
printf("Exit program successfully:\n");
exit(0);
break;
default:
printf("!invalid choice try again:\n");
}
}while(choice!=9);
}
๐ก Doubly Linked List Implementation in C
This project implements a full-featured Doubly Linked List in C, supporting insertion and deletion at various positions, along with display functionality. Each node contains three parts: a pointer to the previous node, data, and a pointer to the next node.
๐ Structure Definition
struct node {
struct node *pre;
int data;
struct node *next;
};
This structure defines a single node in the doubly linked list.
๐ข insert_node_at_begining()
This function inserts a new node at the beginning of the list.
- Allocates memory for a new node using
malloc()
- Updates the
next of new node to point to the current head
- Updates the
pre of the old head (if it exists)
- Makes the new node the new head
๐ข insert_node_at_end()
Inserts a new node at the end of the list.
- If list is empty, sets the new node as head
- Otherwise, traverses to the last node and updates its
next
- Sets the new node’s
pre to the previous last node
๐ข insert_node_at_specific_position()
Inserts a node at a user-specified position.
- Handles special case when position = 1 (inserts at beginning)
- Traverses up to (position - 1)
- Inserts node between
temp and temp->next
- Handles position validation and edge cases
๐ด delete_node_at_first()
Deletes the node at the beginning of the list.
- Updates the head to point to the next node
- Frees memory of the old head
- Handles empty list case
๐ด delete_node_at_end()
Deletes the node at the end of the list.
- Handles single-node case
- Traverses to last node
- Updates
pre->next to NULL and frees last node
๐ด delete_node_at_position()
Deletes a node at a specific position.
- If position is 1, deletes head
- Otherwise, traverses to the node at position
- Updates the
pre and next links accordingly
๐ display()
This function displays the entire list from beginning to end.
- Traverses using
next pointer
- Prints each node in the format:
data <->
๐ง main()
The main() function contains a menu-driven interface to allow user interaction.
- Options: Insert, Delete, Display, Exit
- Uses a
do-while loop and switch-case for selection
- Performs sleep before graceful exit
๐ Summary
This program is a strong example of how doubly linked lists work. Understanding how each node connects forward and backward provides deeper insight into pointer manipulation and memory handling in C.
If you're preparing for a technical interview or practicing DSA in C, mastering linked lists like this is a must!
Comments
Post a Comment