forked from CPRF-Session2/Assignment9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.c
More file actions
156 lines (134 loc) · 3.34 KB
/
linkedlist.c
File metadata and controls
156 lines (134 loc) · 3.34 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Jared Wasserman -- linkedlist.c */
/* This program has various function for a linked list such as adding, removing, printing, comparing, and clearing the whole list.*/
#include <stdio.h>
#include <stdlib.h>
/* Node */
typedef struct node{
int data;
struct node *next;
}node;
/* add */
node* add(node *pointer, int data){
node *newPtr = (struct node*) malloc (sizeof(struct node));
newPtr->next = pointer;
newPtr->data = data;
return newPtr;
}
/* printList */
void printList(node *ptr){
while(ptr->next!=NULL){
printf("%d\n",(*ptr).data);
ptr = ptr->next;
}
}
/* delete */
void delete(node *pointer, int data){
while(pointer->next!=NULL && (pointer->next)->data != data){
pointer = pointer -> next;
}
if(pointer->next==NULL){
printf("%d was not found\n",data);
return;
}
node *tempPtr = pointer -> next;
pointer->next = tempPtr->next;
free(tempPtr);
return;
}
/* clear */
void clear(node *ptr){
node *temp;
while(ptr->next!=NULL){
temp=ptr;
free(ptr);
ptr=temp;
ptr=ptr->next;
}
}
/* smallestLength */
int smallestLength(node *ptr, node *ptr2){
int ptrCount=0;
int ptr2Count=0;
while(ptr->next!=NULL){
ptrCount++;
ptr = ptr->next;
}
while(ptr2->next!=NULL){
ptr2Count++;
ptr2 = ptr2->next;
}
if(ptrCount<=ptr2Count){
return ptrCount;
}else{
return ptr2Count;
}
}
/* smallestSum*/
node* smallestSum(node *ptr, node *ptr2){
node* originalPtr = ptr;
node* originalPtr2 = ptr2;
int ptrSum=0;
int ptr2Sum=0;
while(ptr->next!=NULL){
ptrSum+=ptr->data;
ptr = ptr->next;
}
while(ptr2->next!=NULL){
ptr2Sum+=ptr2->data;
ptr2 = ptr2->next;
}
if(ptrSum<ptr2Sum){
return originalPtr;
}else if(ptrSum==ptr2Sum){
printf("**The two lists have the same sum**\n");
return originalPtr;
}else{
return originalPtr2;
}
}
int main(){
/*Making Linked List*/
struct node *headPtr;
headPtr = (struct node *) malloc (sizeof(struct node));
headPtr->next = NULL;
/*Filling The Linked Lists*/
headPtr = add(headPtr,5);
headPtr = add(headPtr,10);
headPtr = add(headPtr,20);
headPtr = add(headPtr,50);
headPtr = add(headPtr,23);
/*Printing*/
printf("Printing The Linked List:\n");
printList(headPtr);
/*Deleting*/
delete(headPtr,10);
delete(headPtr,50);
/*Print After Deleting 10 & 50*/
printf("\nPrinting The Linked List After Deleting 10 & 50\n");
printList(headPtr);
/*Making Another Linked List*/
struct node *headPtr2;
headPtr2 = (struct node *) malloc (sizeof(struct node));
headPtr2->next = NULL;
/*Filling The Linked List*/
headPtr2 = add(headPtr2,23);
headPtr2 = add(headPtr2,778);
headPtr2 = add(headPtr2,18);
headPtr2 = add(headPtr2,598);
headPtr2 = add(headPtr2,234);
/*Printing*/
printf("\nPrinting The Second Linked List:\n");
printList(headPtr2);
/*Comparing To Find Shortest List*/
printf("\nThe length of the shorter list is: %d\n",smallestLength(headPtr, headPtr2));
/*Comparing To Find Smallest Sum*/
/*Since the function return a pointer rather than printing within the function.
*If the two lists have the same sum, it will print a message but also return the pointer to the first list.
*That is why it print one of the lists and a message saying they are the same if they are equal. */
printf("\nThe list with the smaller sum is:\n");
printList(smallestSum(headPtr, headPtr2));
/*Clearing The Linked List*/
clear(headPtr);
clear(headPtr2);
return 0;
}