-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
141 lines (121 loc) · 1.96 KB
/
Copy pathlinked_list.cpp
File metadata and controls
141 lines (121 loc) · 1.96 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
struct Node *head, *tail, *node;
//creating a node structure
struct Node
{
int data;
struct Node *ptr;
};
//Function for append element or add new node
void append()
{
int x;
printf("\nEnter value to store: ");
scanf("%d", &x);
if (head == NULL)
{
head = (struct Node *)malloc(sizeof(struct Node));
node = head;
}
else
{
node = (struct Node *)malloc(sizeof(struct Node));
tail->ptr = node;
}
node->data = x;
tail = node;
}
//Function to insert element or insert node
void insert()
{
if (head == NULL)
{
append();
}
else
{
int value, index;
printf("\nEnter value to store: ");
scanf("%d", &value);
printf("Enter index: ");
scanf("%d", &index);
struct Node *b_node, *node;
struct Node *a_node = head;
node = (struct Node *)malloc(sizeof(struct Node));
for (int i = 0; i < index; i++)
{
b_node = a_node;
a_node = b_node->ptr;
}
node->data = value;
node->ptr = a_node;
if (index == 0)
head = node;
else
b_node->ptr = node;
}
}
//Function to delete node
void delete_node()
{
int index;
printf("\nEnter Index: ");
scanf("%d", &index);
if(index==0)
head=head->ptr;
else{
struct Node *b_node,*node;
struct Node *a_node = head;
for (int i = 0; i < index; i++)
{
b_node = a_node;
a_node = b_node->ptr;
}
node=a_node->ptr;
b_node->ptr=node;
free(node);
}
}
int main()
{
int choice;
loop:
printf("\tMain Menu\t\n");
printf("1. Append\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter You choice: ");
scanf("%d", &choice);
if (choice == 1)
{
append();
}
else if (choice == 2)
{
insert();
}
else if (choice == 3)
{
delete_node();
}
else if (choice == 4)
{
printf("\n\t");
struct Node *i;
i = head;
while (i != NULL)
{
printf(" %d", i->data);
i = i->ptr;
}
}
else if(choice==5)
return 0;
else
printf("\nEnter Valid Option\n");
printf("\n");
goto loop;
}