Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

Doubly Linked List program in C

Doubly-Linked-List.txt
File size: 3.5 KB

File content type: text/plain

Category: Notes

Course: B.Tech.

Semester: 2 and 3

#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");
}

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;
	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:{
                                        insert_beg(&start,&last);
					printf("Out of insertion\n");
				break;
			}
			case 2:{
					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;
}
//If there are any errors feel free to correct them in the comment Section : )

Added by StAn

Comments
Posted by StAn,

Yes, got an email regarding to this. with link attached. :D

Posted by passhojao,

Live testing comments email notifications. Reply if you see this.