forked from dishanp/Algorithms-For-Software-Developers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDouble Linked List Implementation.c
110 lines (107 loc) · 2.12 KB
/
Double Linked List Implementation.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 data;
struct node *next;
struct node *prev;
};
void create(struct node **head,struct node **last,int n)
{
int el;
struct node *t=NULL;
for(int i=0;i<n;i++)
{
t=(struct node *)malloc(sizeof(struct node));
printf("\n Enter Data No %d:",i+1);
scanf("%d",&t->data);
t->next=NULL;
t->prev=NULL;
if(i==0)
{
*head=*last=t;
(*head)->next=t;
(*head)->prev=t;
}
else
{
(*last)->next=t;
t->prev=*last;
*last=t;
(*last)->next=(*head);
(*head)->prev=t;
}
}
}
void display(struct node *p)
{
struct node *f=p;
printf("\n");
do
{
printf(" %d -> ",p->data);
p=p->next;
}while(p!=f);
}
void insert(struct node **head,struct node **last)
{
printf("Enter element for insertion: ");
struct node *p=NULL;
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=p->prev=NULL;
printf("\nEnter position for insertion: ");
int pos;
scanf("%d",&pos);
if(pos==0)
{
p->next=*head;
*head=p;
(*head)->prev=*last;
(*last)->next=p;
}
else
{
struct node *r=*head;
for(int i=0;i<pos-1;i++)
r=r->next;
p->next=r->next;
p->next->prev=p;
r->next=p;
p->prev=r;
}
}
void delete( struct node **head, struct node **last,int pos)
{
struct node *p;
if(pos==0)
{
struct node *f=*head;
*head=(*head)->next;
(*last)->next=*head;
(*head)->prev=*last;
free(f);
}
else
{
for(int i=0;i<pos-2;i++)
p=p->next;
struct node *q=p->next;
p->next=p->next->next;
p->next->next->prev=p;
free(q);
}
}
int main()
{
struct node *head,*last;
int n;
printf("Enter Size Of LL: ");
scanf("%d",&n);
create(&head,&last,n);
display(head);
//insert(&head,&last);
//display(head);
delete(&head,&last,3);
display(head);
}