forked from gunanksood/C-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoWayLinkedList.c
More file actions
executable file
·134 lines (134 loc) · 2.63 KB
/
TwoWayLinkedList.c
File metadata and controls
executable file
·134 lines (134 loc) · 2.63 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *PREV;
int INFO;
struct node *NEXT;
};
typedef struct node NODE;
void traversef( NODE *FIRST )
{
NODE *PTR = FIRST;
while( PTR != NULL )
{
printf(" [ %d ]",PTR->INFO );
PTR = PTR->NEXT;
}
}
void traverseb( NODE *LAST )
{
NODE *PTR = LAST;
while( PTR != NULL )
{
printf(" [ %d ]",PTR->INFO );
PTR = PTR->PREV;
}
}
void addBeginning(NODE **FIRST , NODE **LAST , int ITEM )
{
NODE *NEW = (NODE*)malloc(sizeof(NODE));
if( NEW == NULL )
{
printf("\n OVERFLOW \n");
}
else
{
NEW->INFO = ITEM;
NEW->PREV = NULL;
if( *FIRST == NULL || *LAST == NULL )
{
*FIRST = *LAST = NEW;
NEW->NEXT = NULL;
}
else
{
NEW->NEXT = *FIRST;
(*FIRST)->PREV = NEW;
*FIRST = NEW;
}
printf("\n New Node is added at Beginning of Two Way Linked List \n");
}
}
void deleteLast( NODE **FIRST , NODE **LAST )
{
NODE *TEMP;
if( *FIRST == NULL || *LAST == NULL )
{
printf("\n UNDERFLOW \n");
}
else
{
TEMP = *LAST;
if( *LAST == *FIRST )
{
*LAST = *FIRST = NULL;
}
else
{
(*LAST)->PREV->NEXT = NULL;
(*LAST) = (*LAST)->PREV;
}
free(TEMP);
printf("\n NODE is REMOVED from last of the Two Way Linked List \n");
}
}
int main()
{
NODE *FIRST = NULL;
NODE *LAST = NULL;
int item , choice , run = 1;
while( run )
{
printf("\n Press 1 For Add New Node at Beginning of Two Way Linked List ");
printf("\n Press 2 For Traverse Forward the Two Way Linked List ");
printf("\n Press 3 For Traverse Backward the Two Way Linked List ");
printf("\n Press 4 For Delete Last Node from the Two Way Linked List ");
printf("\n Press 5 For Exit \n");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch( choice )
{
case 1:
printf("\n Enter Item : ");
scanf("%d",&item);
addBeginning( &FIRST , &LAST , item );
break;
case 2:
printf("\n Content of Two Way Linked List in Forward Manner \n");
if(FIRST == NULL || LAST == NULL )
{
printf(" EMPTY LIST ");
}
else
{
traversef( FIRST );
}
printf("\n");
break;
case 3:
printf("\n Content of Two Way Linked List in Backward Manner \n");
if(FIRST == NULL || LAST == NULL )
{
printf(" EMPTY LIST ");
}
else
{
traverseb( LAST );
}
printf("\n");
break;
case 4:
deleteLast( &FIRST , &LAST );
break;
case 5:
run = 0;
printf("\n Exit From Program \n");
break;
default:
printf("\n Invalid Choice Try Again \n");
break;
}
}
return 0;
}