-
Notifications
You must be signed in to change notification settings - Fork 0
/
20Doubly_linked_list_Program.c
107 lines (91 loc) · 2.36 KB
/
20Doubly_linked_list_Program.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
/*DOUBLY LINKED LIST:-that contains one extra pointer as compare to the node of singly linked list
From any node we can traverse in both the direction*/
//Define Structure means hamra abstract data type
struct node
{
int info;
struct node *prev,*next;
};
struct node* START=NULL; //main pointer
void InsertAsFirstNode(){
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
printf("Enter a Number");
scanf("%d",&n->info);
n->prev=NULL;
n->next=NULL;
printf("Inserted Successfully");
if(START==NULL){
START=n; //ab main start pointer is struct type dynamic node block ko point krne lagega
}
else{
//agr phle se hi kyi sare node hue toh uski adjustment coding hai yeh
START->prev=n; //means start jisko phle block ko point kr raha tha uske previous variable me null tha aur maine usme new block ka address de diya
n->next=START; //means ye ki start me phle block ka address tha right,aur maine new node ke next block me uska address rakh dia
START=n; //means ye ki ab start new node ko point kr rha hai aur hmara new node ke previous block me toh null hai but next block me agle block ka address hai jo hmne isse just oper define kiya hai
}
}
void DeleteFirstNode(){
struct node *r;
if(START==NULL){
printf("Opps ! Doubly list is empty ");
}
else{
r=START;
START=START->next; //next node block ka address start me rakh dia
START->prev=NULL; //prevous me null rakh dia
free(r); //ab esko release kr dia
printf("Deleted Successfully");
}
}
void ViewNodeList(){
struct node *t;
if(START==NULL){
printf("Can't View Coz List is Empty");
}else{
t=START;
while(t->next!=NULL){ //t->next me toh agle block ka adress hai toh ye chlega
printf("%d",t->info);
t=t->next; //remember ab t me next block ka address aa gya hai
}
}
}
int menu(){
int choice;
printf("\n1. Create Node and Add value");
printf("\n2. Delete First Node");
printf("\n3. View List Nodes");
printf("\n4. Exit");
printf("\nENTER YOUR CHOICE");
scanf("%d",&choice);
return (choice);
}
void main(){
while (1)
{
system("cls");
switch (menu())
{
case 1:
InsertAsFirstNode();
break;
case 2:
DeleteFirstNode();
break;
case 3:
ViewNodeList();
break;
case 4:
exit(0);
default:
printf("invaild choice");
break;
}
getch();
}
}
//By Navjot Singh Prince