-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathReverse a Singly Linked List
More file actions
66 lines (66 loc) · 926 Bytes
/
Reverse a Singly Linked List
File metadata and controls
66 lines (66 loc) · 926 Bytes
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* link;
};
typedef struct node* Node;
Node createnode(int info)
{
Node temp=(Node)malloc(sizeof(struct node));
temp->info=info;
temp->link=NULL;
return temp;
}
Node finsert(Node front,int info)
{
Node temp=createnode(info);
if(front == NULL)
front=temp;
else
{
temp->link=front;
front=temp;
}
return front;
}
Node reverse(Node front)
{
Node prev = NULL;
Node cur=front;
Node next=NULL;
while(cur!=NULL)
{
next=cur->link;
cur->link=prev;
prev=cur;
cur=next;
}
return prev;
}
void display(Node front)
{
Node temp=front;
if(front==NULL)
printf("List is empty.");
else
{
while(temp!=NULL)
{
printf("-> %d\t ",temp->info);
temp=temp->link;
}
}
}
void main()
{
int i;
Node front =NULL;
for(i=0;i<5;i++)
front=finsert(front,i);
display(front);
front=reverse(front);
printf("\n");
display(front);
}