Repository navigation
Expand file tree
/
Copy pathlinkedLists.c
More file actions
131 lines (123 loc) · 2.29 KB
/
Copy pathlinkedLists.c
File metadata and controls
131 lines (123 loc) · 2.29 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
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
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node* add(node *head, int num) {
node *temp = (node *)malloc(sizeof(node));
temp->data = num;
temp->next =head;
head = temp;
return head;
}
void printList (node *curr) {
while (curr->next !=NULL)
{
printf("%d\n", curr -> data);
curr = curr->next;
}
}
void delete (node *curr, int rem) {
while (curr->next != NULL && (curr->next)-> data !=rem) {
curr= curr -> next;
}
if (curr ->next ==NULL) {
printf("The number %d is not in the list\n", rem);
return;
}
node *temp = curr -> next;
curr ->next = temp ->next;
free(temp);
return;
}
node* clear (node *curr) {
node *head=curr;
while (head -> next != NULL)
{
head -> next = curr;
}
while (curr -> next != NULL)
{
curr -> next = head;
}
free (curr);
}
void smallestLength (node*l1, node *l2) {
int countL1=0;
int countL2=0;
while (l1 -> next != NULL) {
l1 ->next = l1;
countL1++;
}
while (l2 -> next != NULL) {
l2 ->next = l2;
countL2++;
}
if (countL1> countL2) {
printf("List 1 is bigger\n");
}
if (countL2> countL1) {
printf("List 2 is bigger\n");
}
if (countL1== countL2) {
printf("Lists are the same size\n");
}
}
void smallestSum (node *l1, node *l2) {
int sumL1 = 0;
int sumL2 = 0;
while (l1->next!=NULL) {
l1 ->next = l1;
sumL1+=l1 -> data;
}
while (l2->next!=NULL) {
l2 ->next = l2;
sumL2+=l2 -> data;
}
if (sumL1>sumL2) {
printf("List one has a bigger sum\n");
return;
}
if (sumL2>sumL1) {
printf("List two has a bigger sum\n");
return;
}
if (sumL1==sumL2) {
printf("The lists have equal sums\n");
return;
}
}
int main () {
struct node * head = (node*) malloc(sizeof(node));
head -> next = NULL;
node *curr=head;
head =add(head, 23);
head = add(head, 5);
head = add(head, 8);
head = add(head, 12);
head = add(head, 17);
printList (head);
delete (head, 12);
delete (head, 4);
node *l1;
node *l2;
l1 -> next= NULL;
l2 -> next = NULL;
l1 = add (l1, 34);
l1 = add (l1, 1);
l1 = add (l1, 2);
l1 = add (l1, 43);
l1 = add (l1, 12);
l2 = add (l2, 4);
l2 = add (l2, 9);
l2 = add (l2, 23);
l2 = add (l2, 12);
// smallestSum (l1, l2);
// smallestLength (l1, l2);
printf("test1\n");
head = clear (head);
printf("test2\n");
// printList (head);
return 0;
}