Question

In: Computer Science

Linked Lists Problem 1 Implement a program that reads the following ages (type int) from text...

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)

Solutions

Expert Solution

#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;

}


Related Solutions

Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
1. Design and implement a program that reads a series of integers from the user and...
1. Design and implement a program that reads a series of integers from the user and continually prints their average after every reading. The input stops when the user enters “stop” as the input value. Read each input value as a string, then check if it is “stop” or not. If the string is not “stop”, then, attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException, meaning that the input is not...
JAVA Program The program below the text reads in the pet type …dog, cat or other...
JAVA Program The program below the text reads in the pet type …dog, cat or other (only the words dog, cat, other) and the appointment amount. It is supplied as a beginning code to the assignment: _______________________________________________________________ pet_lab package; import javax.swing.JOptionPane; public class pet_lab { public static void main (String [] args) { String pet, temp; double payment; pet = JOptionPane.showInputDialog (null, "Enter the pet type", "", JOptionPane.QUESTION_MESSAGE); temp = JOptionPane.showInputDialog (null, "Enter the payment for the appointment", "", JOptionPane.QUESTION_MESSAGE);...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT