0% found this document useful (0 votes)
16 views

Double Linklist Creation

This C program implements a singly linked list with functions to create the list, add nodes at the beginning, end, or a specified position, and display the list. The main function uses a switch menu to allow the user to select these list operations and includes functions to get user input for the data and positions. The functions implement adding nodes by dynamically allocating memory for new nodes and updating the next and prev pointers appropriately.

Uploaded by

pd03487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Double Linklist Creation

This C program implements a singly linked list with functions to create the list, add nodes at the beginning, end, or a specified position, and display the list. The main function uses a switch menu to allow the user to select these list operations and includes functions to get user input for the data and positions. The functions implement adding nodes by dynamically allocating memory for new nodes and updating the next and prev pointers appropriately.

Uploaded by

pd03487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

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

typedef node *list;


list head,tail,temp;
void create_list(int,int);
void addatbeg(int);
void addafter(int ,int);
void addatend(int);
void display();
int main()
{

int choice,n,m,position,i;
head=NULL;
while(1)
{
printf("1. Create list\n");
printf("2. Add at begining\n");
printf("3.Add at a position\n");
printf("4. ADD at end\n");
printf("5. Display \n");
printf("Enter your choice : ");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
fflush(stdin);
create_list(m,i);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position at which this element is inserted : ");
scanf("%d",&position);
addafter(m,position);
break;
case 4:
printf("Enter the element : ");
scanf("%d",&m);
addatend(m);
break;

case 5:
display();
break;
case 6:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
getch();
return 0;
}/*End of main()*/

void create_list(int n,int i)


{
if(i==0)
{ /*If list is empty */
temp=(list) malloc(sizeof(node));
temp->data=n;
temp->next=NULL;
temp->prev=NULL;
head=temp;
tail=temp;
}

else
{
temp=(list) malloc(sizeof(node));
temp->data=n;
temp->next=NULL; /*Element inserted at the end */
tail->next =temp;
temp->prev=tail;
tail=tail->next;
}
}/*End of create_list()*/

void addatbeg(int n)
{
list temp;
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->next=head;
head->prev=temp;
head=temp;
}

void addatend(int n)
{
list temp;
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->next=NULL;
tail->next=temp;
temp->prev=tail;
tail=tail->next;
}
void addafter(int n,int pos)
{
list temp,q;
int i;
q=head;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("There are less than %d elements",pos);
return;
}
}/*End of for*/

temp=(list)malloc(sizeof(node) );
temp->data=n;
temp->next=q->next;
q->next=temp;
temp->prev=q;
}
void display()
{
temp=head;
while(temp!=NULL)
{
printf("% d",temp->data);
temp=temp->next;
}
printf("\n");
}

You might also like