-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertdouble.c
80 lines (77 loc) · 1.6 KB
/
insertdouble.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev_link;
int data;
struct node *next_link;
};
void traverse();
void insertbeg();
void insertlast();
void main()
{
struct node *head=(struct node *)malloc(sizeof(struct node));
head->prev_link=NULL;
head->data=10;
head->next_link=NULL;
struct node *current=(struct node*)malloc(sizeof(struct node));
current->prev_link=NULL;
current->data=20;
current->next_link=NULL;
head->next_link=current;
current->prev_link=head;
current=(struct node*)malloc(sizeof(struct node));
current->prev_link=NULL;
current->data=30;
current->next_link=NULL;
head->next_link->next_link=current;
current->prev_link=head->next_link;
traverse(head);
insertbeg(head,70);
insertlast(head,100);
traverse(head);
}
void traverse(struct node *head)
{
struct node *temp=head;
int count=0;
//printf("NULL->");
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next_link;
count++;
}
//printf("\n%d\n",count);
//printf("NULL");
}
void insertbeg(struct node *head,int data)
{
struct node *temp=head;
struct node *new=(struct node *)malloc(sizeof(struct node));
new->prev_link=NULL;
new->data=data;
new->next_link=NULL;
head->prev_link=new;
new->next_link=head;
head=new;
printf("\nAfter\n");
traverse(head);
}
void insertlast(struct node *head,int data)
{
struct node *temp=head;
struct node *new=(struct node *)malloc(sizeof(struct node));
new->prev_link=NULL;
new->data=data;
new->next_link=NULL;
while(temp->next_link!=NULL)
{
temp=temp->next_link;
}
temp->next_link=new;
new->prev_link=temp;
printf("\nLAst\n");
//traverse(head);
}