-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathReverse doubly linked list.c
76 lines (70 loc) · 1.25 KB
/
Reverse doubly linked 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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int id;
struct node *next,*prev;
};
void append(struct node **);
void display(struct node *);
void rev(struct node **pt)
{
struct node *curr=*pt;
while(curr->next!=NULL)
{
curr=curr->next;
}
while(curr!=NULL)
{
printf("%d ",curr->id);
curr=curr->prev;
}
}
int main()
{
struct node *head;
head=NULL;
char choice[10];
do
{
append(&head);
printf("Do you want to add details of another student? Type Yes/No\n");
scanf("%s",choice);
}while(!strcmp(choice,"Yes"));
printf("The details of the students are\n");
display(head);
printf("\n");
rev(&head);
return 0;
}
void append(struct node **head)
{
struct node *newstud=(struct node*)malloc(sizeof(struct node));
struct node *temp=*head;
printf("Enter the id\n");
scanf("%d",&(newstud->id));
newstud->next=NULL;
if(*head==NULL)
{
newstud->prev=NULL;
*head=newstud;
return;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newstud;
newstud->prev=temp;
}
void display(struct node *head)
{
struct node *temp=head;
printf("The values are:\n");
while(temp!=NULL)
{
printf("%d ",temp->id);
temp=temp->next;
}
}