-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5_4.c
99 lines (99 loc) · 1.87 KB
/
lab5_4.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
//Write a C code to convert a doubly linked list into a circular doubly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
} * first;
void create_double(int a);
void display();
void convert();
void displaycircular();
void main()
{
int a;
first = NULL;
int n;
printf("Enter the no of nodes \n");
scanf("%d", &n);
if (n == 0)
{
printf("Empty \n");
}
else
{
for (int i = 0; i < n; i++)
{
printf("Enter data%d \n", i + 1);
scanf("%d", &a);
create_double(a);
}
}
printf("Linkedlist: \n");
display();
convert();
printf("\n circular Linkedlist: \n");
displaycircular();
}
void create_double(int a)
{
struct node *temp = NULL, *ptr = NULL;
ptr = first;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = a;
temp->next = NULL;
temp->prev = NULL;
if (first == NULL)
{
first = temp;
}
else
{
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
temp->prev = ptr;
}
}
void display()
{
struct node *ptr = NULL;
if (first == NULL)
{
printf("Nothing to print \n");
}
ptr = first;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
void convert()
{
struct node *ptr1, *ptr;
ptr = first;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
first->prev = ptr;
ptr->next = first;
}
void displaycircular()
{
struct node *ptr;
ptr = first;
if(first!=NULL)
{
do
{
printf("%d ",ptr->data);
ptr=ptr->next;
} while (ptr!=first);
}
}