-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublelikedlist.c
105 lines (104 loc) · 2.19 KB
/
doublelikedlist.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
#include<stdio.h>
#include<conio.h>
struct node {
int data;
struct node *next;
};
void push(struct node **head,int d){
struct node *temp,*ptr = *head;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = d;
temp->next = *head;
if(*head!=NULL){
while(ptr->next != *head){
ptr = ptr->next;
}
ptr->next = temp;
}else{
temp->next = temp;
}
*head = temp;
return;
}
void append(struct node **head,int d){
struct node *temp,*ptr = *head;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = d;
temp->next = NULL;
if(*head == NULL){
*head = temp;
return;
}
while(ptr->next!=NULL){
ptr = ptr->next;
}
ptr->next = temp;
}
void delete(struct node**head,int key){
struct node *it,*ptr = *head;
if(key == ptr->data && ptr!=NULL){
*head = ptr->next;
ptr->next = NULL;
free(ptr);
return;
}
while(ptr->next!=NULL){
if(ptr->data == key){
break;
}
it=ptr;
ptr=ptr->next;
}
it->next = ptr->next;
free(ptr);
}
void deleteNodeAtK(struct node **head,int k){
struct node *temp = *head,*it;
int i = 0;
if(k == 0){
*head = temp->next;
free(temp);
return;
}
while(temp != NULL){
if(i==k){
it->next = temp->next;
free(temp);
break;
}
i++;
it = temp;
temp = temp->next;
}
}
static void reverse(struct node **head){
struct node *ptr,*temp=*head,*it=NULL;
while(temp!=NULL){
ptr = temp->next;
temp->next = it;
it = temp;
temp = ptr;
}
*head = it;
}
void printList(struct node *head){
struct node *ptr = head;
if(head!=NULL){
do{
printf("%d->",ptr->data);
ptr = ptr->next;
}while(ptr!=head);
}
return;
}
int main(){
struct node *head = NULL;
/* Created linked list will be 11->2->56->12 */
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);
printf("Contents of Circular Linked List\n ");
printList(head);
return 0;
}