Note: Do it Using CPP Language. Thanks. Implement a program that uses dynamic me
ID: 3869162 • Letter: N
Question
Note: Do it Using CPP Language. Thanks.
Implement 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 information (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 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.Explanation / Answer
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 ");
scanf("%d", &n);
printf("Enter the elements ");
for (i = 0; i < n; i++)
{
scanf("%d", &vectorx[i]);
}
printf("Input array elements are ");
for (i = 0; i < n; i++)
{
printf("%d ", vectorx[i]);
}
printf("Enter the element to be deleted ");
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 ");
for (i = 0; i < n - 1; i++)
{
printf("%d ", vectorx[i]);
}
}
else
printf("Element %is not found in the vector ", element);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.