-
Notifications
You must be signed in to change notification settings - Fork 0
/
question6.c
84 lines (79 loc) · 1.67 KB
/
question6.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
// Q6: Adding the even numbers to the beginning of the list
// and odd numbers to the end of the list until -1 is entered from keyboard.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int number;
struct node *next;
};
typedef struct node node;
node *first, *last;
int length = 0;
node insertFirst(int num)
{
node *newNode;
newNode = (node *)malloc(sizeof(node));
newNode->number = num;
if (length == 0)
{
first = last = newNode;
newNode->next = NULL;
}
else
{
newNode->next = first;
first = newNode;
}
}
node insertLast(int num)
{
node *newNode;
newNode = (node *)malloc(sizeof(node));
newNode->number = num;
if (length == 0)
{
last = first = newNode;
newNode->next = NULL;
}
else
{
last->next = newNode;
newNode->next = NULL;
last = newNode;
}
}
void print()
{
node *print = first;
while (print != NULL)
{
printf(" %d", print->number);
print = print->next;
}
}
int main()
{
printf("Enter odd or even numbers and press -1 when you wont to stop entering\n");
int num;
while (1)
{
printf("(%d) Enter a number : ", length + 1);
scanf("%d", &num);
if (num == -1)
break;
else if (num % 2 == 0)
{
insertFirst(num);
length++;
}
else
{
insertLast(num);
length++;
}
}
printf ("\nthe EVEN number in beginning and the ODD number in the end of the list :\n");
print();
return 0;
}