0% found this document useful (0 votes)
17 views7 pages

Linked List

The document contains C code for a doubly linked list implementation. It includes functions to create nodes at the beginning, end, and at a specified position, as well as a function to display the list. The main function provides a menu for user interaction to perform these operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Linked List

The document contains C code for a doubly linked list implementation. It includes functions to create nodes at the beginning, end, and at a specified position, as well as a function to display the list. The main function provides a menu for user interaction to perform these operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include<stdio.

h>

#include<stdlib.h>

struct node

int data;

struct node *next;

struct node *prev;

};

struct node *head=NULL,*tail=NULL;

int pos;

void create_node_begin()

int num;

struct node *newnode;

newnode=(struct node *)malloc(sizeof(struct node));

if(newnode==NULL)

printf("\n list is empty");

printf("\nInput the data of node:");

scanf("%d",&num);

newnode->data=num;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)
{

head=newnode;

tail=newnode;

else

newnode->next=head;

head->prev=newnode;

head=newnode;

void create_node_end()

int num;

struct node *newnode;

newnode=(struct node *)malloc(sizeof(struct node));

if(newnode==NULL)

printf("\n list is empty");

printf("\nInput the data of node:");

scanf("%d",&num);

newnode->data=num;

newnode->next=NULL;
newnode->prev=NULL;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

void create_node_position(int pos)

struct node *newnode,*c,*p;

int num,i;

newnode=(struct node *)malloc(sizeof(struct node));

if(newnode==NULL)

printf("\n memory not allocated");

printf("\nInput the data of node:");

scanf("%d",&num);

newnode->data=num;

newnode->next=NULL;
newnode->prev=NULL;

if(pos==1)

head->prev=newnode;

newnode->next=head;

head=newnode;

return;

c=head;

for(i=1;i<=pos-1&&c!=NULL;i++)

p=c;

c=c->next;

if(c==NULL)

printf("\nposition out of range");

free(newnode);

return;

p->next=newnode;

newnode->next=c;

c->prev=newnode;

newnode->prev=p;

}
void display_list()

struct node *temp;

temp=head;

if(temp==NULL)

printf("list is empty\n");

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

printf("NULL\n");

int main()

int ch;

while(ch!=5)

printf("\n double linked list");

printf("\n1.create at begin");

printf("\n2.create at end");

printf("\n3.create at position");
printf("\n4.display");

printf("\n5.exit");

printf("\n choice");

scanf("%d",&ch);

if(ch==1)

create_node_begin();

else if (ch==2)

create_node_end();

else if(ch==3)

printf("\nenter the position:");

scanf("%d",&pos);

create_node_position(pos);

else if(ch==4)

display_list();

else if(ch==4)

printf("\n Exiting....");
}

else

printf("\n invalid choice");

break;

You might also like