Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is P2-1 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SingleLinkedListAllinOne { class Node { public int element; public Node link; public Node()
This is P2-1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SingleLinkedListAllinOne { class Node { public int element; public Node link; public Node() { element = -1; } public Node(int n) { element = n; } } class LinkedList { public Node header; public LinkedList() { header = null; } // ------------------------------------------------------------- public bool isEmpty() // true if no nodes { return header == null; } // ------------------------------------------------------------- public Node Search(int KEY) { Node current = header; while (current != null && current.element != KEY) current = current.link; return current; } public void ShowLinkedList() { if (header == null) Console.WriteLine("The list is empty!"); else { Node current = header; Console.Write("{0}->", current.element); while (!(current.link == null)) { current = current.link; Console.Write("{0}->", current.element); } Console.Write("null"); Console.WriteLine(); } } public Node searchPrevious(int KEY) { if (header == null) return header; else { Node current = header; if (current.element == KEY) return null; while (!(current.link == null) && (current.link.element != KEY)) current = current.link; return current; } } public void Insert(int newItem, int preKey) { Node current; Node newNode = new Node(newItem); current = Search(preKey); newNode.link = current.link; current.link = newNode; } public void Delete(int KEY) { if (header.element == KEY) header = header.link; else { Node p = searchPrevious(KEY); if (p.link == null) Console.WriteLine("there is no such item."); if (!(p.link == null)) p.link = p.link.link; } } public void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public void Remove() { if (header != null) header = header.link; } } class SingleLinkedList { static int ShowMenu() { Console.WriteLine(" \tLinked List Testing Program "); Console.WriteLine(" Enter the operation number: (1)Output (2)Insert (3)Search (4)Remove (5)Deletion (6)Append (7)Exit "); return int.Parse(Console.ReadLine()); } static void Main(string[] args) { LinkedList LL = new LinkedList(); int sel = 0; while (sel != 7) { sel = ShowMenu(); switch (sel) { case 1: //Output Console.WriteLine("The current data in the list: "); LL.ShowLinkedList(); break; case 2: //Insert if (LL.isEmpty()) { Console.WriteLine("Empty list...Append first!"); break; } Console.WriteLine("Before insert: "); LL.ShowLinkedList(); Console.Write("Enter a value to insert: "); int x = int.Parse(Console.ReadLine()); Console.Write("Enter where to insert: "); int pos = int.Parse(Console.ReadLine()); LL.Insert(x, pos); Console.WriteLine("After insert: "); LL.ShowLinkedList(); break; case 3: //Search Console.Write("Enter target to search for: "); int target = int.Parse(Console.ReadLine()); Node found = LL.Search(target); if (found != null) Console.WriteLine("{0} is found.", found.element); else Console.WriteLine("{0} is not found.", target); break; case 4: //Remove Console.WriteLine("Before remove: "); LL.ShowLinkedList(); LL.Remove(); Console.WriteLine("After remove: "); LL.ShowLinkedList(); break; case 5: Console.WriteLine("Before delete: "); LL.ShowLinkedList(); Console.WriteLine("Enter the number to delete: "); int d = int.Parse(Console.ReadLine()); LL.Delete(d); Console.WriteLine("After delete: "); LL.ShowLinkedList(); break; case 6: Console.Write("Enter value to append: "); int x_appd = int.Parse(Console.ReadLine()); LL.Append(x_appd); LL.ShowLinkedList(); break; case 7: Console.WriteLine("End of the testing. Goodbye!\a\a"); break; default: Console.WriteLine("Invalid input!"); break; } } /////////////////////////////////////////////////////////////// } } }
4. (Programming) Use P2-1 SingleLinedList as a reference, add the following operations in the class LinkedList; Find the average data values of the linked list. Find the item with largest key, and then delete the node. Test ALL operations in the Main method. (Also display the average of the data values of the linked list, the largest key, the linked list before and after deleting the node with the largest key; 5. (Programming) Complete the Main method in program P2-2 DoubleLinkedList to te oubleLinkedList class. Make your program menu-driven ons defined i Requirement for assignment reports The cover page must be attached. For each programming question, the s description of the program, and running page (screen shot) which shows that the program works for all cases. Add proper comments lines for non-trivial so that the instructors can read and understand the program. 1. 2. n must contain the program itself, proper 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started