Question

Note: Do it Using CPP Language. Thanks.

Objectives Program Description Inplement a program that uses dynamic memory and uses an insertion sort to add items to a singly -linked list. . • Modify the sorted listed by deleting elements as specified below • Create a sorted list (by state) of nodes containing all the state infor mation (state name, capital, population). • Display your list. • Remove states whose population is less than four (4) million . • Display the resulting list after the states have been removed from the list • Create a sorted list (by population) of nodes containing all the state information. • Display your list. • Remove states whose population is greater than 10 million. • Display the resulting list after the states have been removed from the list Data file – do not modify (estimated state populations as of 2000): http://www2.cs.uidaho.edu/bruceb/cs120/Assignments/states2000. dat Deliverables • A complete functional program with output. • A program design sheet. Describe all functions necessary to implement your program. • Programming Log: Record the time required to design and implement your program. Record of things you encountered/learned while implementing your program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java implementation

public class SinglyLinkedList {


public void addLast(SinglyLinkedListNode newNode) {
if (newNode == null)
return;
else {
newNode.next = null;
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
}

public void addFirst(SinglyLinkedListNode newNode) {
if (newNode == null)
return;
else {
if (head == null) {
newNode.next = null;
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
}

public void insertAfter(SinglyLinkedListNode previous,
SinglyLinkedListNode newNode) {
if (newNode == null)
return;
else {
if (previous == null)
addFirst(newNode);
else if (previous == tail)
addLast(newNode);
else {
SinglyLinkedListNode next = previous.next;
previous.next = newNode;
newNode.next = next;
}
}
}
}


C++ implementation

void SinglyLinkedList::addLast(SinglyLinkedListNode *newNode) {
if (newNode == NULL)
return;
else {
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
}

void SinglyLinkedList::addFirst(SinglyLinkedListNode *newNode) {
if (newNode == NULL)
return;
else {
if (head == NULL) {
newNode->next = NULL;
head = newNode;
tail = newNode;
} else {
newNode->next = head;
head = newNode;
}
}
}

void SinglyLinkedList::insertAfter(SinglyLinkedListNode *previous,
SinglyLinkedListNode *newNode) {
if (newNode == NULL)
return;
else {
if (previous == NULL)
addFirst(newNode);
else if (previous == tail)
addLast(newNode);
else {
SinglyLinkedListNode *next = previous->next;
previous->next = newNode;
newNode->next = next;
}
}
}

======================================================================

import java.util.Scanner;
public class Delete
{
public static void main(String[] args)
{
int n, x, flag = 1, loc = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the element you want to delete:");
x = s.nextInt();
for (int i = 0; i < n; i++)
{
if(a[i] == x)
{
flag =1;
loc = i;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
for(int i = loc+1; i < n; i++)
{
a[i-1] = a[i];
}
System.out.print("After Deleting:");
for (int i = 0; i < n-2; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n-2]);
}
else
{
System.out.println("Element not found");
}
}
}


void main()
{
int vectorx[10];
int i, n, pos, element, found = 0;

printf("Enter how many elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &vectorx[i]);
}
printf("Input array elements are\n");
for (i = 0; i < n; i++)
{
printf("%d\n", vectorx[i]);
}
printf("Enter the element to be deleted\n");
scanf("%d", &element);
for (i = 0; i < n; i++)
{
if (vectorx[i] == element)
{
found = 1;
pos = i;
break;
}
}
if (found == 1)
{
for (i = pos; i < n - 1; i++)
{
vectorx[i] = vectorx[i + 1];
}
printf("The resultant vector is \n");
for (i = 0; i < n - 1; i++)
{
printf("%d\n", vectorx[i]);
}
}
else
printf("Element %is not found in the vector\n", element);

Add a comment
Know the answer?
Add Answer to:
Note: Do it Using CPP Language. Thanks. Implement a program that uses dynamic memory and uses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a...

    Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a linked list data structure. Gives the user the following options:                         1. To create and add the first node to a linked list.          2. To add a single node to the pre-existing linked list.             The list must already exist before a new node can be added.            The nodes should be maintained in ascending order based on the data value within the nodes...

  • Write a C++ -program that will implement Worst-fit, memory management algorithms. Your program must do the...

    Write a C++ -program that will implement Worst-fit, memory management algorithms. Your program must do the following: 1. Input the memory size and the number and sizes of all the partitions (limit the max number of the partitions to 5); 2. Input the job list that includes: - Job's name; - Job's size. 3. For each job you should create in your program a data structure that will include the job status (Run/Wait) and the partition number (if the job...

  • Requesting help with the following C Program: DESIGN and IMPLEMENT a menu driven program that uses...

    Requesting help with the following C Program: DESIGN and IMPLEMENT a menu driven program that uses the following menu and can perform all menu items: Enter a payroll record for one person Display all paycheck stubs Display total gross payroll from all pay records. Quit program The program will reuse the DATE struct from the previous assignment.  You should also copy in the functions relating to a DATE. The program will create a PAYRECORD struct with the following fields: typedef struct...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

  • This program needs to be in JAVA language. Also, I need to create my own LinkedList...

    This program needs to be in JAVA language. Also, I need to create my own LinkedList class, I can not use the Java LinkedList Library. Please read all aspects of the program, it needs to display 200 random numbers that are 5 digits long sorted from smallest to largest. I can not use Collection(s) or packages libraries. Should only need: import java.util.Random; import java.util.Scanner; Please provide output screenshots after the code. Thank you! You are going to create a Linked...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Assignment Description. In this assignment, you will design a class to represent a book of recipes...

    Assignment Description. In this assignment, you will design a class to represent a book of recipes using a doubly linked list. Your recipe book will support methods to add a recipe and get a recipe at a position; your main will use these to print all the names of all the recipes in the book. Create a C++ class called RecipeBook. Your class must implement the following variables, constructors, and methods: Create a private inner class called Node for implementing...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Its a report . Implement and test a preliminary version of the polynomial class, using a...

    Its a report . Implement and test a preliminary version of the polynomial class, using a small array to store the coefficients of the polynomial in order of increasing exponents, following the design pattern of object-oriented approach. Suppose the range of the exponents is in [0, 30). • Use an array as a private member variable. • insert(), adding a new term on specific exponent. • remove(), deleting a term. • Have a add() method, deciding the parameters and the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT