-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversing-list.c
84 lines (83 loc) · 1.56 KB
/
reversing-list.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
#include <stdlib.h>
#include <stdio.h>
struct node {
int info;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR getnode(){
NODEPTR p;
p = (NODEPTR)malloc(sizeof(struct node));
return p;
}
void insafter(NODEPTR p, int x){
NODEPTR q;
if(p==NULL){
printf("void insertion\n");
exit(1);
}
q = getnode();
q->next = p->next;
p->next = q;
q->info = x;
}
void delafter(NODEPTR p){
NODEPTR q;
if(p->next==NULL){
printf("void deletion\n");
exit(1);
}
q = getnode();
q = p->next;
p->next = q->next;
free(q);
}
void yazdir(NODEPTR p){
NODEPTR q;
if(p->next==NULL){
printf("bos liste\n");
exit(1);
}
q = getnode();
q = p->next;
while(q!=NULL){
printf("%d-",q->info);
q = q->next;
}
printf("\n");
free(q);
}
void reverse(NODEPTR p){
if(p->next==NULL){
printf("empty list\n");
exit(1);
}
NODEPTR A=getnode(),B=getnode(),q=getnode(),temp=getnode();
A = B = q = p;
temp = p->next;
while(p!=NULL){
B = p->next;
p->next = A;
A = p;
p = B;
}
q->next = A;
temp->next = NULL;
}
int main(){
NODEPTR liste;
liste = getnode();
liste->next = NULL;
printf("listeye kac eleman eklemek istersiniz? ");
int size;
scanf("%d",&size);
int temp;
for(int i=0;i<size;i++){
printf("%d. eleman: ",i+1);
scanf("%d",&temp);
insafter(liste,temp);
}
yazdir(liste);
reverse(liste);
yazdir(liste);
}