C Program to Perform Following operation in Double Linked List

Write a program in c to perform following operations on double linked list 

1. Add number in the linked list

2. Insert the number at specific position

3. See the list

4. Insert at last position

5. Insert in beginning of the list


Double Linked List Problems


Program

#include<stdio.h>
#include<stdlib.h>
struct node{
	int data;
	struct node *next;
	struct node *prev;
};

struct node *head=NULL;
int count=1;

struct node* newnode()
{
	struct node *ptr;
	ptr=(struct node*)malloc(sizeof(struct node));
	if(ptr==NULL)
	{
		printf("Memory is not avaliable:\n");
		return 0;
	//	break;
	 }
	 else
	 {
	 	printf("Enter the number of the node:");
	 	scanf("%d",&ptr->data);
	 	ptr->next=NULL;
	 	ptr->prev=NULL;
	 	if(head==NULL)
	 	{
	 		head=ptr;
		}
	 }
	 count=count+1;
	 return ptr;
}

int insertbeg()
{
	struct node* ptr;
	ptr=newnode();
	if(count==2)
	return 1;
	//break;
	else
	{
		ptr->next=head;
		head->prev=ptr;
		head=ptr;
	}
}

int insertlast()
{
	struct node *ptr;
	ptr=newnode();
	struct node *temp;
	temp=head;
	if(count==2)
	{
	head=ptr;
	return 1;
   }
	//break;
	else
	{
	while(temp->next!=NULL)
	{
		temp=temp->next;
	}
	ptr->prev=temp;
	temp->next=ptr;
    }
}

int insertpos()
{
	int pos;
	printf("Enter the Position at which you want to insert:\n");
	scanf("%d",&pos);
	if(pos>count)
	{
		printf("ERROR:Position is not avaliable:\n");
	}
	struct node *ptr;
	if(pos==1)
	{
		insertbeg();
	}
	ptr=newnode();
	struct node *temp;
	temp=head;
	int i=1;
	while(i<(pos-1))
	{
		temp=temp->next;
		i++;
	}
	if(temp->next==NULL)
	{
		insertlast;
		return 1;
		//break;
	}
	else
	{
		ptr->prev=temp;
		ptr->next=temp->next;
		temp->next->prev=ptr;
		temp->next=ptr;
	}
}

int display()
{
	struct node *ptr;
	ptr=head;
	if(head==NULL)
	{
		printf("ERROR:NO element is in the linked list:\n");
		//break;
		return 1;
	}
	printf("Number of the linked list is:\n");
	struct node *temp;
	while(ptr!=NULL)
	{
		printf("%d\n",ptr->data);
		temp=ptr;
		ptr=ptr->next;
	}
	printf("The number of linked list in reverse order:\n");
	while(temp!=NULL)
	{
		printf("%d\n",temp->data);
		temp=temp->prev;
	}
}
int main()
{
	int choice=1;
	while(choice)
	{
		printf("Enter 1 to add number in the linked list:\n");
		printf("Enter 2 to insert the number at specific position:\n");
		printf("Enter 3 to see the list:\n");
		printf("Enter 4 to insert at last position:\n");
		printf("Enter 5 to insert in begaining of the list:\n");
		printf("Enter 0 to end the program:\n");
		scanf("%d",&choice);
		if(choice==1)
		insertlast();
		else if(choice==2)
	    insertpos();
		else if(choice==3)
		display();
		else if(choice==4)
		insertlast();
		else if(choice==5)
		insertbeg();
	}
	return 0;
}


Output

Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
1
Enter the number of the node:12
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
2
Enter the Position at which you want to insert:
1
Enter the number of the node:134
Enter the number of the node:32
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
3
Number of the linked list is:
134
32
12
The number of linked list in reverse order:
12
32
134
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
4
Enter the number of the node:56
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
3
Number of the linked list is:
134
32
12
56
The number of linked list in reverse order:
56
12
32
134
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
5
Enter the number of the node:1000
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
3
Number of the linked list is:
1000
134
32
12
56
The number of linked list in reverse order:
56
12
32
134
1000
Enter 1 to add number in the linked list:
Enter 2 to insert the number at specific position:
Enter 3 to see the list:
Enter 4 to insert at last position:
Enter 5 to insert in begaining of the list:
Enter 0 to end the program:
0

Admin

Hi This is the Admin of CodingSoln. Currently Pursuing B. Tech Computer Science and Engineering form KIIT University India

Post a Comment

Previous Post Next Post