Skip to content

Commit 4d4ce48

Browse files
Merge pull request #295 from Rounak14/master
doublyLL.c
2 parents 724674e + d0f7589 commit 4d4ce48

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Linked List/doublyLL.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef struct _node
5+
{
6+
int val;
7+
struct _node *prev,*next;
8+
}node;
9+
10+
node *makenode(int);
11+
void print(node *);
12+
node *append(node *,node **,int);
13+
14+
void main()
15+
{
16+
node *start='\0',*last='\0',*nd;
17+
int n;
18+
while(1)
19+
{
20+
printf("\nEnter an item : ");
21+
scanf("%d",&n);
22+
if(n==0)
23+
{
24+
break;
25+
}
26+
start=append(start,&last,n);
27+
}
28+
print(start);
29+
30+
}
31+
32+
void print(node *ptr)
33+
{
34+
if(ptr&&ptr->next)
35+
{
36+
while(ptr)
37+
{
38+
printf("%d ",ptr->val);
39+
ptr=ptr->next;
40+
}
41+
printf("\n");
42+
}
43+
else
44+
{
45+
while(ptr)
46+
{
47+
printf("%d ",ptr->val);
48+
ptr=ptr->prev;
49+
}
50+
printf("\n");
51+
}
52+
}
53+
54+
node *makenode(int n)
55+
{
56+
node *nd;
57+
nd=(node *)malloc(sizeof(node));
58+
nd->val=n;
59+
nd->prev=nd->next='\0';
60+
return nd;
61+
}
62+
node *append(node *start,node **last,int n)
63+
{
64+
node *nd,*ptr;
65+
ptr=start;
66+
if(ptr=='\0')
67+
{
68+
nd=makenode(n);
69+
start=nd;
70+
*last=nd;
71+
}
72+
else
73+
{
74+
while(ptr!=*last)
75+
{
76+
ptr=ptr->next;
77+
}
78+
nd=makenode(n);
79+
ptr->next=nd;
80+
nd->prev=ptr;
81+
*last=nd;
82+
}
83+
return start;
84+
}

0 commit comments

Comments
 (0)