-
Notifications
You must be signed in to change notification settings - Fork 0
/
question2.c
76 lines (69 loc) · 1.56 KB
/
question2.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
// Q2: A singly linear list stores integer values in each node and has multiple nodes.
// Write a function using given prototype below.
// This function cuts the last node of the list and adds it to the beginning as first node.
// It takes beginning address of the list as a parameter and returns the updated list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int number;
struct node *next;
};
typedef struct node node;
node *insertFirst()
{
node *head, *last;
for (int i = 0; i < 4; i++)
{
if (i == 0)
{
head = (node *)malloc(sizeof(node));
last = head;
}
else
{
last->next = (node *)malloc(sizeof(node));
last = last->next;
}
printf("enter a number : ");
scanf("%d", &last->number);
}
last->next = NULL;
return head;
}
void print(node *head)
{
node *printer;
printer = head;
while (printer != NULL)
{
printf(" %d", printer->number);
printer = printer->next;
}
}
node *cutlastaddhead(node *head)
{
node *last, *temp;
temp = last = head;
while (last->next !=NULL)
{
last = last->next;
}
while (temp->next != last)
{
temp = temp->next;
}
last->next = head;
head = temp->next;
temp->next = NULL;
return head;
}
int main()
{
node *head;
head = insertFirst();
print(head);
head = cutlastaddhead(head);
printf ("\n");
print(head);
}