Our Android App Also Got an Upgrade. Download now!

Doubly Linked List program in C

Doubly-Linked-List.txt

File size: 3.5 KB

File content type: text/plain

Category: Practical Files

Subject: Data Structure

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

struct node
{
	int id;
	char name[30];
	struct node* next;
	struct node* back;
} *start,*ptr,*last;


void insert_beg(struct node **start,struct node **last)
{
	struct node * tmp=(struct node*)malloc(sizeof(node));

	if(tmp==NULL)
	{
		printf("OverFlow\n");
		return;
	}
	printf("Enter Details Name then id\n");
	scanf("%s%d",tmp->name,&tmp->id);

	if(*start==NULL)
	{
		tmp->back=NULL;
		tmp->next=NULL;
		*start=tmp;
		*last=tmp;
		
		printf("Succesfully added as First node\n");
		return;	
	}
	
	tmp->next=*start;

	tmp->back=NULL;
	(*start)->back=tmp;
	*start=tmp;
	
	printf("Succesfully added in the Beginning \n");
}


/*OUT OF SYLLABUS

void insert_last(struct node **start,struct node **last)
{
	struct node * tmp=(struct node*)malloc(sizeof(node));

	if(tmp==NULL)
	{
		printf("OverFlow\n");
		return;
	}
	printf("Enter Details Name then id(addition at last)\n");
	scanf("%s%d",tmp->name,&tmp->id);

	if(*start==NULL)
	{
		tmp->back=NULL;
		tmp->next=NULL;
		*start=tmp;
		*last=tmp;
		
		printf("Succesfully added as first node\n");
		return;	
	}

	tmp->back=*last;
	tmp->next=NULL;
	(*last)->next=tmp;
	*last=tmp;

	printf("Succesfully added (last) \n");
}


void del_beg(struct node **start,struct node **last)
{
	if(*start==NULL)
	{
		printf("Underflow,List empty CANT DELETE\n");
		return;
	}
	if(*start==*last)
	{
		printf("Succesfully deleted EK hee node tha(beg)\n");
		*last=NULL;
		free(*start);
		*start=NULL;
		
		return;
	}
	struct node *tmp;
	struct node *delu;

	tmp=(*start)->next;
	delu=*start;
	tmp->back=NULL;
	*start=tmp;
	free(delu);
	
	(*start)->back=NULL;
	
	printf("Succesfully deleted (beg)\n");
}

*/

void del_last(struct node **start,struct node **last)
{
	if(*start==NULL)
	{
		printf("Underflow,List empty CANT DELETE\n");
		return;
	}
	if(*start==*last)
	{
		printf("Succesfully deleted EK hee node tha (last)\n");
		*last=NULL;
		free(*start);
		*start=NULL;
		return;
	}
	struct node *tmp;
	struct node *delu;

	tmp=(*last)->back;
	delu=*last;
	tmp->next=NULL;
	*last=tmp;
	free(delu);
	
	printf("Succesfully deleted (last)\n");
}
void disp(struct node **start,struct node **last)
{
	struct node *tmp;
	tmp=*start;
	
	while(tmp!=NULL)
	{
		printf("Name = %s then ID = %d\n",tmp->name,tmp->id);
		tmp=tmp->next;
	}
	
	printf("Done printing\n");
}
int main()
{
	start=last=NULL;
	
	int con,ch,what;
	do
	{
		printf("Menu Driven ---> \n Enter 1 to Insert\t Enter 2 to Delete\n Enter 3 to Display\n");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1:{
					printf("1 for Beginning\t2 for End\tINSERT\n");
					scanf("%d",&what);
					if(what==1)
					{
						insert_beg(&start,&last);
						
					}
					else
					{
						printf("Error Out of Syllabus\n"); //insert_last(&start,&last); 
						
					}
					printf("Out of insertion\n");
				break;
			}
			case 2:{
					printf("1 for Beginning\t 2 for End\t DELETE\n");
					scanf("%d",&what);
					if(what==1)
					{
						printf("Error Out of Syllabus\n"); //del_beg(&start,&last);
						
					}
					else
					{
						del_last(&start,&last);
						
					}
					printf("Out of deletion\n");
				break;
			}
			case 3:{
					printf("Display\n");
					disp(&start,&last);
					printf("Out of Display\n");
				break;
			}
		}
		printf("Enter 1 to continue\n");
		scanf("%d",&con);
	}while(con==1);
	printf("End of prog..\n");
	return 0;
}

 

Program for doubly linked list

Last Updated: July 16, 2022