-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedlist.c
127 lines (118 loc) · 2.39 KB
/
linkedlist.c
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>
struct node{
int data;
struct node* link;
};
struct node* root = NULL;
int length(void);
void append(void);
void display(void);
void addAtBegin(void);
void delete(void);
int main()
{
int val;
printf("Hello, World!\n");
printf("enter Element");
for(int i= 0;i<=3; i++){
append();
}
val = length();
addAtBegin();
val = length();
display();
delete();
display();
return 0;
}
void append(void){
struct node* temp;
temp = (struct node*) malloc(sizeof(struct node));
printf("Enter the data Value\n");
scanf("%d",&temp->data);
temp->link = NULL;
if(root == NULL){
root = temp;
}
else{
struct node* p;
p = root;
while(p->link!=NULL){
p = p->link;
}
p->link = temp;
}
}
void addAtBegin(void){
struct node* temp;
temp = (struct node*) malloc(sizeof(struct node));
printf("Enter the data Value\n");
scanf("%d",&temp->data);
temp->link = NULL;
if(root == NULL){
root = temp;
}
else{
temp->link = root;
root = temp;
}
}
void display(void){
struct node* temp;
temp = (struct node*) malloc(sizeof(struct node));
//printf("Enter the data Value\n");
// scanf("%d", temp->data);
//temp->link = NULL;
temp = root;
if(temp == NULL){
printf("No Element");
}
else{
while(temp!=NULL){
printf("%d-->",temp->data);
temp = temp->link;
}
printf("\n");
}
}
int length(void){
int count = 0;
struct node* temp;
temp = (struct node*) malloc(sizeof(struct node));
temp = root;
while(temp!=NULL){
count++;
temp = temp->link;
}
printf("lenth is : %d\n", count);
return count;
}
void delete(void){
struct node* temp;
temp = (struct node*) malloc(sizeof(struct node));
int loc;
printf("enter Location to delete\n");
scanf("%d",&loc);
if(loc>length()) {
printf("Envalid Loc");
}
else if(loc == 1){
temp = root;
root = temp->link;
temp->link = NULL;
free(temp);
}
else{
struct node* p = root, *q;
int i = 1;
while(i< loc-1){
p = p->link;
i++;
}
q=p->link;
p->link = q->link;
q->link = NULL;
free(q);
}
}