In: Computer Science
Linked Lists
Problem 1
Implement a program that reads the following ages (type int) from
text file ages.txt and stores
them in a linked list:
16 18 22 24 15 31 27 19 13
Implement the class LinkedList along with any applicable member
functions (e.g. void
insert(int num)).
Perform the following actions on the list (if applicable, use
recursion):
1. Display the data in the list
2. Insert 18 at the front of the list
3. Insert 23 at the end of the list
4. Insert 25 after the number 24
5. Compute the average age
6. Sort the ages in ascending order
7. Determine how many people are 18 or older
Problem 2
Implement a program that reads the following data (two strings, one
double) from text file
balances.txt and stores it in a linked list:
John Miller 2508.13
Carolyn Spacer 188.54
Mark Lee 1816.82
Sophia Brown 3200.68
Jim Gomez 12068.34
Carlos Mendoza 783.41
Erica Robertson 2000.67
Implement the class LinkedList along with any applicable member
functions (e.g. void
moveToEnd(string fullName)).
Perform the following actions on the list:
1. Display the data in the list
2. Remove the person with the lowest balance
3. Display the name and balance of the person with the most
money
4. Display the average balance of people with a balance of $2,000
or more
5. Find Sophia Brown and move her to the end of the list (use
multipurpose helper
functions to make this easier)
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int value;
struct Node *next;
};
void insert_in_front(struct Node **head,int data)
{
// allocate new node
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
//put the data into it
new_node->value = data;
//since it is new head, attached the linked list to it's back
new_node->next = *head;
// make it new head
*head = new_node;
}
void insert_in_end(struct Node **head, int data)
{
//allocate the new node
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
//put the data into it
new_node->value = data;
//since it is going to be last node, it's next pointer will be null
new_node->next = NULL;
struct Node *last = *head;
// if initial list was null the new_node will become head of the linked list
if(*head==NULL)
{
*head = new_node;
return;
}
// traverse to end of the list
while(last->next!=NULL)
{
last = last->next;
}
// adding the node to end of the list
last->next = new_node;
return;
}
void insert_after(struct Node **head,int prev_data,int data)
{
//allocate new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
//put data into it
new_node->value = data;
//finding the previous node
struct Node* prev_node = *head;
while(prev_node->value!=prev_data)
{
prev_node = prev_node->next;
}
//make next of new node as next of previous node
new_node->next = prev_node->next;
//make the next of previous node as head of new node
prev_node->next = new_node;
return ;
}
void display(struct Node* node)
{
while(node->next!=NULL)
{
printf("%d -> ",node->value);
node = node->next;
}
printf("%d\n",node->value);
}
float average_age(struct Node* node)
{
if(node==NULL)return 0;
int sum=0;
int count=0;
while(node!=NULL)
{
sum+=node->value;
count++;
node=node->next;
}
return (float)sum/count;
}
int greater_than(struct Node* node,int value)
{
int ans=0;
while(node!=NULL)
{
if(node->value>=18)ans++;
node=node->next;
}
return ans;
}
void swap(struct Node* a,struct Node* b)
{
int temp = a->value;
a->value = b->value;
b->value = temp;
}
void sort(struct Node *start)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
/* Checking for empty list */
if (start == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
if (ptr1->value > ptr1->next->value)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
int main()
{
struct Node* head = NULL;
FILE *fs;
char ch, buffer[32];
int i = 0, arr[100], j = 0;
// Openning the file with file handler as fs
fs = fopen("ages.txt", "r");
// Read the file unless the file encounters an EOF
while(1){
// Reads the character where the seeker is currently
ch = fgetc(fs);
// If EOF is encountered then break out of the while loop
if(ch == EOF){
break;
}
//skipping the space character
else if(ch == ' '){
// Converting the content of the buffer into
// an array position
arr[j] = atoi(buffer);
// Increamenting the array position
j++;
bzero(buffer, 32);
i = 0;
// then continue
continue;
}
else{
// reads the current character in the buffer
buffer[i] = ch;
i++;
}
}
// printing out all the elements that are stored in the
// array of integers
for(i = 0; i < j; i++){
printf("%d ", arr[i]);
}
printf("\n\n");
for(int i=0; i<j; i++)
{
insert_in_end(&head,arr[i]);
}
printf("List of ages:\n");
display(head);
insert_in_front(&head,18);
printf("\nList after inserting 18 in front:\n");
display(head);
insert_in_end(&head,23);
printf("\nList after inserting 23 in end:\n");
display(head);
insert_after(&head,24,25);
printf("\nList after inserting 25 after 24:\n");
display(head);
printf("\nAverage age:%f\n",average_age(head));
sort(head);
printf("\nList after sorting in ascending order:\n");
display(head);
printf("\nNo. of people of age 18 or greater: %d",greater_than(head,18));
return 0;
}