forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinklist.cpp
More file actions
155 lines (141 loc) · 1.99 KB
/
Copy pathlinklist.cpp
File metadata and controls
155 lines (141 loc) · 1.99 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdlib.h>
#include <stdio.h>
struct linked_list{
int data;
struct linked_list* next;
};
typedef struct linked_list* node;
node head = NULL;
void AddNode(int value){
node temp, p;
temp = NULL;
temp = (node)malloc(sizeof(struct linked_list));
temp -> data = value;
temp -> next = NULL;
if(head == NULL){
head = temp;
}
else{
p = head;
while(p-> next != NULL){
p = p->next;
}
p -> next = temp;
}
}
void BegNode(int value)
{
node temp;
temp = NULL;
temp = (node)malloc(sizeof(struct linked_list));
temp -> data = value;
temp -> next = NULL;
if(head == NULL){
head = temp;
}
else
{
temp->next=head;
head=temp;
}
}
void printlist(){
node p1;
p1 = head;
printf("->");
while(p1){
printf("%d->", p1->data);
p1 = p1 -> next;
}
}
void insert(int pos, int val){
node newptr = NULL;
node ptr = head;
int steps = 1;
newptr = (node)malloc(sizeof(struct linked_list));
newptr->data = val;
while(steps < pos - 1){
ptr = ptr -> next;
steps++;
}
/*newptr -> next = ptr->next;
ptr->next = newptr;
*/
if(pos == 1){
newptr-> next = head;
head = newptr;
}
else{
newptr-> next = ptr -> next;
ptr -> next = newptr;
}
}
void del_list(){
node ptr;
while(head != NULL){
ptr = head;
head = head -> next;
free(ptr);
}
}
void del(int pos){
node p = head, temp;
if(pos == 1){
temp = head;
head = head -> next;
free(temp);
}
else{
for(int i=2; i<pos; i++){
p = p -> next;
}
temp = p -> next;
p -> next = temp -> next;
free(temp);
}
}
void print(node p)
{
if(p==NULL)
{
return;
}
else
{
printf("%d->",p->data);
print(p->next);
}
}
void revprint(node p)
{
if(p==NULL)
{
return;
}
else
{
print(p->next);
printf("%d->",p->data);
}
}
int main(){
AddNode(2);
AddNode(3);
AddNode(4);
AddNode(5);
BegNode(12);
print(head);
printf("\n");
revprint(head);
insert(1,10);
insert(3,7);
printf("\n");
printlist();
del(3);
printlist();
printf("\n");
del_list();
printf("\n");
printlist();
return 0;
}