-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubleylinkedlist.c
More file actions
69 lines (68 loc) · 1.69 KB
/
doubleylinkedlist.c
File metadata and controls
69 lines (68 loc) · 1.69 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
#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*next;
struct node*prev;
};
void traverse1(struct node* head){
struct node*p = head;
do{
printf("Elements %d \n" , p->data);
p=p->next;
}while(p != NULL);
}
void traverse2(struct node*head){
struct node *p = head;
while(p!= NULL && p->next != NULL){
p=p->next;
}
while(p!=NULL){
printf("Elements opposite %d\n" ,p->data );
p=p->prev;
}
}
struct node * reverse(struct node* head){
struct node *current = head;
struct node*temp = NULL;
while (current!= NULL){
temp = current->prev;
current -> prev = current -> next;
current -> next = temp;
current = current -> prev;
}
if(temp != head){
head = temp->prev;
}
return head;
}
int main (){
struct node *head ;
struct node *second;
struct node * third;
struct node * fourth ;
head = (struct node *)malloc (sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node *)malloc(sizeof(struct node));
fourth = (struct node*)malloc (sizeof(struct node));
head -> data = 32;
head -> next = second;
head -> prev = NULL;
second -> data = 72;
second -> next = third;
second -> prev = head;
third -> data = 992;
third -> next = fourth;
third -> prev = second;
fourth -> data = 320;
fourth -> next = NULL;
fourth -> prev = third;
traverse1(head);
printf(" \n");
traverse2(head);
printf(" \n");
printf("Again we will reverse the list and traverse it from starting index \n");
head = reverse(head);
traverse1(head);
return 0;
}