-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ass3_q1.c
124 lines (96 loc) · 2.17 KB
/
Ass3_q1.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
============================================================================
Name : Ass3_q1.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
typedef struct list
{
int intone;
float floatone;
struct list* next;
}myList;
#include <stdio.h>
#include <stdlib.h>
static myList *createList();
void concatenate(myList *ptr1, myList *ptr2);
int countLists(myList *p);
int main(void) {
myList *list1, *list2;
/*create two linked lists and check for null*/
list1 = createList();
if(NULL == list1)
{
return EXIT_FAILURE;
}
list2 = createList();
if(NULL == list2)
{
return EXIT_FAILURE;
}
/*show size of each linked lists*/
printf("list 1 contains %d linked lists\n", countLists(list1));
printf("list 2 contains %d linked lists\n", countLists(list2));
/*concatenate list2 onto list1*/
concatenate(list1, list2);
/*print new size of list 1 after adding list 2 onto it*/
printf("list 1 now contains %d linked lists after concatenation\n", countLists(list1));
}
static myList *createList()
{
myList *p = NULL, *head = NULL;
int num = 0;
printf("Please enter number of lists to create for the linked list:\n");
fflush(stdout);
scanf("%d", &num);
for(int i = 0; i < num; i++)
{
if(NULL == head)
{
/*create head if it doesn't exist*/
head = (myList *)malloc(sizeof(myList));
if(NULL == head)
{
printf("mem fail");
}
/*assign head to first p*/
p = head;
}
else
{
/*allocate memory for next list*/
p->next = (myList *)malloc(sizeof(myList));
if(NULL == p->next)
{
return NULL;
}
/*move pointer to the list just created*/
p = p->next;
}
/*assign the last list to point to NULL*/
}
p->next = NULL;
return head;
}
int countLists(myList *p){
int count = 0;
while(p != NULL)
{
count++;
p = p->next;
}
return count;
}
void concatenate(myList *ptr1, myList *ptr2)
{
/*find the address of last list on linked list 1*/
while(ptr1->next != NULL)
{
ptr1 = ptr1->next;
}
/*make the last list on list 1 point to the head of list 2*/
ptr1->next = ptr2;
}