-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinsert a node double linklist.cpp
127 lines (127 loc) · 2.78 KB
/
insert a node double linklist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
struct node *pre;
int info;
struct node *next;
}*new_node,*start,*last,*temp;
void create()
{
int value,choice;
do{
new_node=(struct node*)malloc(sizeof(struct node));
printf("Enter element : ");
scanf("%d",&value);
new_node->pre=NULL;
new_node->info=value;
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
last=new_node;
}
else
{
last->next=new_node;
new_node->pre=last;
last=new_node;
}
printf("Wnat to add more for 1\0");
scanf("%d",&choice);
}while(choice);
}
void display()
{
printf("your linklist in right direction : ");
printf("NULL<->");
temp=start;
while(temp->next!=NULL)
{
printf("%d<->",temp->info);
temp=temp->next;
}
printf("%d<->",temp->info);
printf("NULL\n");
printf("your linklist in left direction : ");
printf("NULL<->");
temp=last;
while(temp->pre!=NULL)
{
printf("%d<->",temp->info);
temp=temp->pre;
}
printf("%d<->",temp->info);
printf("NULL");
}
void insert()
{
int choice,pos,item,i;
printf("***choice***\n1.insert at beg\n2. insert at end\n3. insert at specfic postion\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter element : ");
scanf("%d",&item);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("out of memory\n");
exit(1);
}
new_node->info=item;
new_node->pre=NULL;
new_node->next=start;
start->pre=new_node;
start=new_node;
break;
case 2:
printf("enter element : ");
scanf("%d",&item);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("out of memory\n");
exit(1);
}
new_node->info=item;
new_node->next=NULL;
new_node->pre=last;
last->next=new_node;
last=new_node;
break;
case 3:
printf("enter element and positon: ");
scanf("%d%d",&item,&pos);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("out of memory\n");
exit(1);
}
new_node->info=item;
temp=start;
for(i=0;i<pos-1;i++)
{
temp=temp->next;
}
new_node->next=temp->next;
temp->next->pre=new_node;
temp->next=new_node;
new_node->pre=temp;
break;
default:
printf("wrong choice");
break;
}
}
int main()
{
create();
display();
printf("\n");
insert();
display();
}