-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path86dcllInsertFirst.c
More file actions
80 lines (72 loc) · 1.51 KB
/
86dcllInsertFirst.c
File metadata and controls
80 lines (72 loc) · 1.51 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
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node* prev;
int data;
struct node* next;
};
struct node* newNode(int no);
int insertFirst(struct node** src,struct node** tail,int no);
void display(struct node**);
struct node* findTail(struct node**);
int main(){
int itr1,num,data;
struct node* head=NULL,*tail=NULL;
printf("Insert number of nodes to be created.\n");
scanf("%d",&num);
for(itr1=0;itr1<num;itr1++){
printf("Enter element %d: ",itr1+1);
scanf("%d",&data);
insertFirst(&head,&tail,data);
}
display(&head);
return 0;
}
int insertFirst(struct node** src,struct node** tail,int no){
struct node* temp1=*src;
struct node* temp2;
if(*src==NULL){
temp1=newNode(no);
temp1->prev=temp1->next=temp1;
*src=temp1;
*tail=temp1->prev;
return 0;
}else{
temp2=newNode(no);
temp2->next=temp1;
temp2->prev=temp1->prev;
temp1->prev=temp2;
temp2->prev->next=temp2;
*src=temp2;
}
}
struct node* findTail(struct node** head){
struct node* temp=*head;
if(*head==NULL){
printf("list is empty.\n");
return NULL;
}
while(temp->next!=*head){
temp=temp->next;
}
return temp;
}
void display(struct node** head){
struct node* temp=*head;
if(*head==NULL){
printf("List is empty.\n");
return;
}
printf("Output:\n");
while(temp->next!=*head){
printf("|%d|<=>",temp->data);
temp=temp->next;
}
printf("|%d|\n",temp->data);
}
struct node* newNode(int no){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->data=no;
new->next=new->prev=NULL;
return new;
}