-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
118 lines (94 loc) · 3.57 KB
/
linked_list.c
File metadata and controls
118 lines (94 loc) · 3.57 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void create(int data){
struct node *newnode = (struct node *)malloc(sizeof(newnode));
newnode -> data = data;
newnode -> next = NULL;
if (head == NULL)
{
head = newnode;
tail = newnode;
}
else
{
tail->next = newnode;
tail = tail->next;
}
//printf("element inserted");
}
void display(){
struct node *current = head;
if (head == NULL){
printf("Empty Linked List");
}
printf("Elements of Linked list are:\n");
while(current !=NULL)
{
printf("%5d\t", current->data);
printf("%5ld\t",(long)current->next);
printf("\n");
current = current->next;
}
}
void insertatbegin(){
struct node *newnode = (struct node *)malloc(sizeof(struct node));
printf("enter a new node value:");
scanf("%d",&newnode->data);
newnode->next = head;
head = newnode;
printf("Elements of Linked list are:\n");
while(newnode !=NULL)
{
printf("%5d\t", newnode->data);
printf("%5ld\t",(long)newnode->next);
printf("\n");
newnode = newnode->next;
}
}
void insertatpos(int pos){
struct node *newnode = (struct node *)malloc(sizeof(struct node));
struct node *current = head;
struct node *temp = head;
//int pos;
//printf("enter pos:");
//scanf("%d",&pos);
printf("enter a new node value:");
scanf("%d",&newnode->data);
int i;
for (i= 1; i< pos-1; i++)
{
current = current -> next ;
}
newnode -> next = current -> next;
current -> next = newnode;
while(temp !=NULL)
{
printf("%5d\t", temp->data);
printf("%5ld\t",(long)temp->next);
printf("%5ld\t",(long)temp);
printf("\n");
temp = temp->next;
}
}
int main(){
create(10);
create(20);
create(30);
create(40);
display();
int pos;
printf("enter pos:");
scanf("%d",&pos);
if (pos == 0){
insertatbegin();
}
else if (pos != 0){
insertatpos(pos);
}
return 0;
}