Question

The Task: Create a console application that prompts the user to select either a stack, queue,...

The Task:

Create a console application that prompts the user to select either a stack, queue, linked list, or binary search tree. Then prompt the user for a number that will then be passed to a recursive function that will populate an array in ascending order. If user selected binary search tree, they will then be prompted to choose how the data will be received either in-order, preorder, or post-order. If user selected linked list, they will then be prompted to choose if the data will be printed in ascending or descending order. The program will then use the array to create the chosen storage type and then print out the results to the console. (C#)

C#

What You Need to Do:

Implement the code so that the output produced follows as below:

I have included a few examples:

Example 1:

What type of storage would you like to use?

Linked list

What’s the maximum numbers?

17

How would you like the data returned?

Ascending

Here’s your returned number list:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17

Example 2:

What type of storage would you like to use?
Stack

What’s the maximum numbers?
9

Here’s your returned number list:

9 8 7 6 5 4 3 2 1

Example 3:

What type of storage would you like to use?
BST

What’s the maximum numbers?
12

How would you like the data returned?
Pre-order

Here’s your returned number list:
1 2 4 8 9 5 10 11 3 6 12 7

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

//////////////////////// project name is type4

////////////////////////////////////////////////////////////////

class Node
{
private int data;
private Node left;
private Node right;

public Node() {
this.data = 0;
this.left = null;
this.right = null;
}

public Node(int d)
{
this.data = d;
this.left = null;
this.right = null;
}

public void setdata(int a) { this.data = a; }
public void setLeft(Node n) { this.left = n; }
public void setRight(Node n) { this.right = n; }


public int getdata() { return data; }
public Node getLeft() { return left; }
public Node getRight() { return right; }

}

///////////////////////////////////////////////////////////////

class BST
{
private Node root;
private int size;

public BST()
{
this.root =null;
this.size = 0;
}

public void addData(int data)
{
Node node = new Node(data);
if (this.root == null) {
this.root = node;
}
else {
Node ur = this.root;

while (ur != null)
{
if (ur.getdata() > data)
{
if (ur.getLeft() == null)
{
ur.setLeft(node);
break;
}
else
{
ur = ur.getLeft();
}
}
else
{
if (ur.getRight() == null)
{
ur.setRight(node);
break;
}
else
{
ur = ur.getRight();
}
}
}
}
this.size++;
}


public void inorder() { this.inorder(root); }
public void preorder() { this.preorder(root); }
public void postorder() { this.postorder(root); }

public int getSize()
{
return this.size;
}

public void inorder(Node node) {
if (node == null)
{
return;
}
else
{
inorder(node.getLeft());
Console.Write(node.getdata()+" ");
inorder(node.getRight());
}
}
public void preorder(Node node) {
if (node == null)
{
return;
}
else
{

Console.Write(node.getdata() + " ");
inorder(node.getLeft());
inorder(node.getRight());
}
}
public void postorder(Node node) {
if (node == null)
{
return;
}
else
{
inorder(node.getLeft());
inorder(node.getRight());
Console.Write(node.getdata() + " ");
}
}
}

///////////////////////////////////////////////////////////////

class Queue
{
private int[] data;
private int size;
private int index;
private int indexf;

public Queue(int s)
{
this.data = new int[s];
this.size = s;
this.index = 0;
this.indexf = 0;
}

public void push(int d)
{
if (this.index < this.size )
{
this.data[index] = d;
this.index++;
}
}

public int top()
{
return this.data[indexf];
}

public int pop()
{
indexf++; ;
return this.data[indexf-1];
}

public int getSize()
{
return (this.index-this.indexf);
}

public void printData()
{
while (this.getSize() != 0)
{
int data_ = this.pop();
Console.Write(data_ + " ");
}
}

}

/////////////////////////////////////////////////////////////

class Stack
{
private int[] data;
private int size;
private int index;

public Stack(int s)
{
this.data = new int[s];
this.size = s;
this.index = 0;
}

public void push(int d)
{
if (this.index < this.size )
{
this.data[index] = d;
this.index++;
}
}

public int top()
{
return this.data[index-1];
}

public int pop()
{
index--;
return this.data[index];
}

public int getSize()
{
return this.index;
}

public void printData()
{
while (this.getSize() !=0)
{
int data_ = this.pop();
Console.Write(data_ + " ");
}
}

}

////////////////////////////////////////////////////////////

class LinkedList
{
class Node
{
private int data;
private Node next;

public Node(int d)
{
this.data = d;
this.next = null;
}

public void setData(int d) { this.data = d; }
public void setNext(Node n) { this.next = n; }

public int getData() { return data; }
public Node getNext() { return next; }
}


private Node root;

public LinkedList()
{
this.root = null;
}

public void addData(int s)
{
Node node = new Node(s);
if (root == null)
{
this.root = node;
}
else
{
node.setNext(this.root); ;
this.root = node;
}
}


public int getSize()
{
int size = 0;
Node cur = root;
while (cur!=null)
{
size++;
cur = cur.getNext();
}
return size;
}

public void printAcending() {
int[] dataLst = new int[this.getSize()];
int i = 0;
Node cur = this.root;

while (cur != null)
{
dataLst[i++] = cur.getData();
cur = cur.getNext();
}

Array.Sort(dataLst);

for(i = 0; i < dataLst.Length; i++)
{
Console.Write(dataLst[i]+" ");
if (i < dataLst.Length - 1)
{
Console.Write(",");
}
}

}
public void printDecending() {
int[] dataLst = new int[this.getSize()];
int i = 0;
Node cur = this.root;

while (cur != null)
{
dataLst[i++] = cur.getData();
cur = cur.getNext();
}

Array.Sort(dataLst);
Array.Reverse(dataLst);


for (i = 0; i < dataLst.Length; i++)
{
Console.Write(dataLst[i] + " ");
if (i < dataLst.Length - 1)
{
Console.Write(",");
}
}
}
}

//////////////////////////////////////////////////////////////// main function

class Program
{
static void Main(string[] args)
{
Console.WriteLine("What type of storage would you like to use?");
String type=Console.ReadLine();

  
Console.WriteLine("What’s the maximum numbers?");
String number = Console.ReadLine();
int n = Convert.ToInt16(number);

if (type.ToLower().Equals("stack") == true) {
Stack stack = new Stack(n);
for(int i = 1; i <= n; i++)
{
Console.WriteLine("Data "+i);
String data = Console.ReadLine();
stack.push(Convert.ToInt16(data));
}
Console.WriteLine("Here’s your returned number list:");
stack.printData();
}
if (type.ToLower().Equals("queue") == true) {
Queue queue = new Queue(n);
for (int i = 1; i <= n; i++)
{
Console.WriteLine("Data " + i);
String data = Console.ReadLine();
queue.push(Convert.ToInt16(data));
}
Console.WriteLine("Here’s your returned number list:");
queue.printData();
}
if (type.ToLower().Equals("bst") == true) {
BST bst = new BST();
for (int i = 1; i <= n; i++)
{
Console.WriteLine("Data " + i);
String data = Console.ReadLine();
bst.addData(Convert.ToInt16(data));
}
  
Console.WriteLine("How would you like the data returned?");
String rtpe = Console.ReadLine();

Console.WriteLine("Here’s your returned number list:");
if (rtpe.ToLower().Equals("pre-order") == true) {
bst.preorder();
}
if (rtpe.ToLower().Equals("post-order") == true) {
bst.postorder();
}
if (rtpe.ToLower().Equals("in-order") == true) {
bst.inorder();
}

  
}
if (type.ToLower().Equals("linked list") == true) {
LinkedList lnklst = new LinkedList();
for (int i = 1; i <= n; i++)
{
Console.WriteLine("Data " + i);
String data = Console.ReadLine();
lnklst.addData(Convert.ToInt16(data));
}

Console.WriteLine("How would you like the data returned?");
String rtpe = Console.ReadLine();

Console.WriteLine("Here’s your returned number list:");
if (rtpe.ToLower().Equals("ascending") == true)
{
lnklst.printAcending();
}
if (rtpe.ToLower().Equals("decending") == true)
{
lnklst.printDecending();
}

}

Console.ReadKey();

}
}

what type of storage would you like to use? Stack Whats the maximum numbers? Data 1 6 Data 2 23 Data 3 33 Data 4 12 Data 5 9

What type of storage would you like to use? queue Whats the maximum numbers? 11 Data 1 4 Data 2 34 Data 3 91 Data 4 22 Data

What type of storage would you like to use? Linked list Whats the maximum numbers? 8 Data 1 12 Data 2 45 Data 3 564 Data 4 2

what type of storage would you like to use? Linked List Whats the maximum numbers? Data 1 345 Data 2 564 Data 3 754 Data 4 1

45 What type of storage would you like to use? BST Whats the maximum numbers? 11 Data 1 145 Data 2 75 Data 3 15 Data 4 178 D

what type of storage would you like to use? BST Whats the maximum numbers? 11 Data 1 45 Data 2 75 Data 3 5 Data 4 78 Data 5

Add a comment
Know the answer?
Add Answer to:
The Task: Create a console application that prompts the user to select either a stack, queue,...
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
  • Array list, linked list, Stack, Queue, Binary tree, Hash table At least one application example combining...

    Array list, linked list, Stack, Queue, Binary tree, Hash table At least one application example combining data structure and algorithm should be given.

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • Using Java Create an application that creates and displays a list of names using an array....

    Using Java Create an application that creates and displays a list of names using an array. Then modify the application to create and use a list of numbers. Console for the names application Please enter a name or type “exit” to quit: Diane Please enter a name or type “exit” to quit: Joe Please enter a name or type “exit” to quit: Greta Please enter a name or type “exit” to quit: Henry Please enter a name or type “exit”...

  • Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

    Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow...

    help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Exercises: 1) Which of the following applications may use a stack? A. parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above. 2) Consider the usual algorithm for determining whether a sequence of parentheses is balanced....

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • Using C++ Requirements Note: “deque” STL container is not allowed, for the simplicity for graders. Queue Queue is a firs...

    Using C++ Requirements Note: “deque” STL container is not allowed, for the simplicity for graders. Queue Queue is a first-in-first-out (FIFO) data structure. For the Queue container, we will implement a program that simulates a buffer. The simulation needs a function that randomly generate number. And the buffer simulation starts with no value in the buffer. At the start of simulation, the menu should ask user: how many rounds will be simulated. the percentage chance to put a randomly generated...

  • Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array...

    Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array named partNumbers that you used for task 3. Sort the array in ascending order. 1065, 1095, 1075, 1055, 1056, 1090, 1098, 1088, 1097, and 1078. Java: use Array.sort() C#: use Array.Sort() PHP: use sort() Write code that asks the user to enter two part numbers. For C#, use console input. Implement a binary tree search function called binarySearch() to search the array for 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