-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyInsertion.cpp
126 lines (121 loc) · 2.55 KB
/
SinglyInsertion.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int main()
{
struct node
{
int data;
struct node *link;
};
struct node *head,*newnode,*temp;
head = 0;
int choice = 1;
int count=0;
while(choice)
{
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter data : ");
scanf("%d",&newnode->data);
newnode->link = 0;
if(head == 0)
{
head = temp = newnode;
}
else
{
temp->link = newnode;
temp = newnode;
}
printf("Do you want to continue 0/1 ?");
scanf("%d",&choice);
if(choice==1)
{
continue;
}
else
{
break;
}
}
temp = head;
while(temp!=0)
{
printf("%d->",temp->data);
temp = temp->link;
count++;
}
printf("NULL\n");
printf("Total no. of nodes : %d",count);
//here display the link list is complete.
// insert at beginning is start from here.
int count1=count+1;
newnode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter the data U want to insert at begin : ");
scanf("%d",&newnode->data);
newnode->link = head;
head = newnode;
temp = head;
while(temp!=0)
{
printf("%d->",temp->data);
temp = temp->link;
count++;
}
printf("NULL\n");
printf("Total no. of nodes : %d",count1);
//here display the instert at beginning link list is complete.
// insert at ending is start from here.
newnode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data U want to insert at end : ");
scanf("%d",&newnode->data);
newnode->link = 0;
temp =head;
while(temp->link!=0)
{
temp = temp->link;
}
temp->link=newnode;
temp = head;
while(temp!=0)
{
printf("%d->",temp->data);
temp = temp->link;
count++;
}
printf("NULL\n");
printf("Total no. of nodes : %d",count1+1);
//here display the insert at ending link list is complete.
// insert at after a given location is start from here.
int pos,i=1;
newnode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter the position after which U insert at : ");
scanf("%d",&pos);
if(pos>count+1)
{
printf("invalid Position '~' ");
}
else
{
temp=head;
while(i<pos)
{
temp=temp->link;
i++;
}
printf("\nEnter the data U want to insert at : ");
scanf("%d",&newnode->data);
newnode->link=temp->link;
temp->link=newnode;
temp=head;
while(temp!=0)
{
printf("%d->",temp->data);
temp = temp->link;
count++;
}
printf("NULL\n");
printf("Total no. of nodes : %d",count1+2);
}
return 0;
}