-
Notifications
You must be signed in to change notification settings - Fork 121
/
Forward_and Backward_Navigation_DLL.c
110 lines (89 loc) · 2.21 KB
/
Forward_and Backward_Navigation_DLL.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
105
106
107
108
109
110
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* left;
struct node* right;
};
typedef struct node *NODE;
NODE insert(NODE, int);
NODE navigate(NODE);
int main()
{
NODE first =NULL; //This is the first node of the doubly linked list
int page, ch;
for (;;)
{
printf("\nPress 1:Insert Page 2:Navigate else Exit\n");
scanf("%d",&ch);
switch(ch){
case 1: printf("Enter Page: "); //Page number is entered
scanf("%d",&page);
first= insert(first, page);
break;
case 2: first = navigate(first);
break;
default: exit(0);
}
}
}
NODE insert(NODE first, int page){
NODE newnode, pres; //Here 'pres' is the present node
newnode = (NODE) malloc (sizeof(struct node));
newnode -> info = page;
newnode -> left = newnode -> right =NULL;
if(first == NULL){
first = newnode;
return first;
}
pres = first;
while(pres->right != NULL)
pres = pres -> right;
newnode -> left = pres;
pres -> right = newnode; //Node is inserted at the front position
return first;
}
NODE navigate(NODE first){
int ch;
NODE pres = first;
for (;;)
{
printf("\nNavigate:: 1:Backward 2:Forward else Main Menu\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(first==NULL) //For Backward Navigation
{
printf("Navigation not possible.\n");
return first;
}
else if(pres->left == NULL)
printf("Backward Navigation not possible\n");
else
{
printf("User is at %d page\t",pres->info);
pres = pres -> left;
printf("User moved to %d page\n",pres->info);
}
first = pres;
break;
case 2: if (first == NULL) //for Forward Navigation
{
printf("Navigation not possible\n");
return first;
}
else if (pres->right == NULL)
printf("Forward Navigation is not possible\n");
else
{
printf("User is at %d page\t",pres->info);
pres=pres->right;
printf("User moved to %d page\n",pres->info);
}
first = pres;
break;
default: return first;
}
}
}