0% found this document useful (0 votes)
15 views4 pages

Create Malloc

The document contains a C program that implements a doubly linked list with functions to create nodes, insert nodes at specific positions, delete nodes, and display the list. It allows users to interact with the list through a menu-driven interface. The program includes error handling for invalid positions during insertion and deletion operations.

Uploaded by

stdsx146nissimt
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)
15 views4 pages

Create Malloc

The document contains a C program that implements a doubly linked list with functions to create nodes, insert nodes at specific positions, delete nodes, and display the list. It allows users to interact with the list through a menu-driven interface. The program includes error handling for invalid positions during insertion and deletion operations.

Uploaded by

stdsx146nissimt
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<malloc.h>

struct node
{
int data;
struct node* prev;
struct node* next;
};
struct node*tail=NULL;
struct node*head=NULL;
struct node*new=NULL;
struct node*temp=NULL;
int a;

void create(int a)
{
new=(struct node*)malloc(sizeof(struct node));
new->prev=NULL;
new->next=NULL;
new->data=a;
if (head==NULL)
{
head=new;
tail=new;
}
else
{
tail->next=new;
new->prev=tail;
tail=new;
}
}

void inssp()
{
int count=0;
int pos;
int i=1;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
printf("enter pos ");
scanf("%d",&pos);
if(pos<=0 || pos>count+1)
{
printf("invalid pos");
}
else if(pos==1)
{//insertbeg();
}
else if(pos==count+1)
{
//insertend();
}
else
{
new=(struct node*)malloc(sizeof(struct node));
new->prev=NULL;
new->next=NULL;
printf("enter val");
scanf("%d",&(new->data));
struct node*temp1=head;
struct node*temp2=head;
while(i!=pos)
{
temp2=temp1;
temp1=temp1->next;
i++;
}
new->prev=temp2;
new->next=temp1;
temp2->next=new;
temp1->prev=new;
}

void delsp()
{
int count=0;
int pos;
int i=1;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
printf("enter pos ");
scanf("%d",&pos);
if(pos<=0 || pos>count)
{
printf("invalid pos");
}
else if(pos==1)
{//delbeg();
}
else if(pos==count)
{
//deleteend();
}
else
{
struct node*temp1=head;
struct node*temp2=head;
while(i!=pos)
{
temp2=temp1;
temp1=temp1->next;
i++;
}
printf("deleting %d at pos %d",temp1->data,pos);
temp2->next=temp1->next;
temp1->next->prev=temp2;
temp1->next=NULL;
temp1->prev=NULL;
free(temp1);
}}

void display()
{
temp=head;
while(temp!=NULL)
{
printf("%d",(temp->data));
temp=temp->next;

}
}

void main()
{
create(10);
create(20);
140
create(30);
141
create(40);
142
int ch;
143
do{
144
printf("what u wanna do?\n1 to insert sp\n 2 to del sp \n3 to see\n4 to
145
scanf("%d",&ch);
146
switch(ch)
147
{
148
case 1:
149
{
150
inssp();
151
break;
152
}
153
case 2:
154
{
155
delsp();
156
break;
157
}
158
case 3:
159
{
160
display();
161
break;
162
}
163
}
164
}while(ch!=4);
165

You might also like