forked from neilchauhan2/Git_first
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlistBasic.c
104 lines (77 loc) · 1.91 KB
/
linkedlistBasic.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
#include <stdio.h>
#include <stdlib.h>
void main(){
struct Node{
int data;
struct Node *next;
};
void printList(struct Node *x){
while(x != NULL){
printf("%d ",x->data);
x = x-> next;
}
}
int reverseList(struct Node *x){
struct Node *curr = (struct Node*)malloc(sizeof(struct Node));
struct Node *pre = (struct Node*)malloc(sizeof(struct Node));
struct Node *nex = (struct Node*)malloc(sizeof(struct Node));
curr = x;
pre = nex = NULL;
while(curr != NULL){
nex = curr -> next;
curr -> next = pre;
pre = curr;
curr = nex;
}
x = pre;
return x;
}
void searchInsert(struct Node *head, int value){
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
struct Node *q = (struct Node*)malloc(sizeof(struct Node));
p -> data = 7;
q = p;
while(head -> next != NULL){
if(head -> data == value){
q -> next = p;
p -> next = head;
break;
}
q = head;
head = head -> next;
}
}
void push(struct Node *head, int value){
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
while(head -> next != NULL){
head = head -> next;
}
head->next = p;
p -> data = value;
p->next = NULL;
}
void pop(struct Node *head){
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
while(head -> next != NULL){
p = head;
head = head -> next;
}
p -> next = NULL;
}
struct Node *head = (struct Node*)malloc(sizeof(struct Node));
struct Node *two = (struct Node*)malloc(sizeof(struct Node));
struct Node *three = (struct Node*)malloc(sizeof(struct Node));
struct Node *four = (struct Node*)malloc(sizeof(struct Node));
struct Node *five = (struct Node*)malloc(sizeof(struct Node));
head -> data = 1;
head -> next = two;
two -> data = 2;
two -> next = three;
three -> data = 3;
three -> next = four;
four -> data = 4;
four -> next = five;
five -> data = 5;
five -> next = NULL;
printf("Linked list");
}