Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey, this is my assingment; by C# 1. Create a Generic LinkedList and test your LinkedList by inserting and removing nodes. (50 points) Include at

Hey,

this is my assingment; by C#

1. Create a Generic LinkedList and test your LinkedList by inserting and removing nodes. (50 points)

Include at least the following methods:

bool IsEmpty() (determine if your list is empty or not)

void InsertHeadNode(T headvalue) (insert a node with value headvalue to the front)

void InsertTailNode(T tailvalue) (insert a node with value tailvalue to the end)

T RemoveHeadNode () (remove the first Node and return its value)

T RemoveTailNode () (remove the last Node and return its value)

void PrintList() (print the value of all nodes in the list)

void InsertAfter (Node p, T newvalue) (insert a new node with value newvalue after node p )

void RemoveAfter (Node p ) (remove the node after node p )

Node Find(T target) ( find and return the first occurrence of a node with value target)

--------------------------------------------

Since most of you are confused about removeatLast() method for the linked list, here is a code snippet to give you some hints. Instead of creating a doubly linked list and previous links to find the previous node, you may do the following instead to get to the second last node. This way you still create a singly linked list (less complicated)

tail = current; //create a tail node to point to the last node to be deleted

current = head; // point to the head node

//Locate the node before tail node

while (current.next != tail) // and until you come to the one before the tail node

{ current = current.next; // keep pointing to the next one in line }

tail = current; // when you've exited that loop, you've reached the one whose "next" is the tail

tail.next = null; // and that one is now the last one in the list, we've eliminated the old tail

count--;

--------------------------

----------------------------------------------------------

Code example that what Pro. did it to explain what is linklist is. In one class

----------------------------------------------------------

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace CsharpGenericLinkedList { public class Example { public static void Main() { // Create the link list. string[] words = { "the", "fox", "jumped", "over", "the", "dog" }; LinkedList sentence = new LinkedList(words); Display(sentence, "The linked list values:"); Console.WriteLine("sentence.Contains(\"jumped\") = {0}", sentence.Contains("jumped"));

// Add the word 'today' to the beginning of the linked list. sentence.AddFirst("today"); Display(sentence, "Test 1: Add 'today' to beginning of the list:");

// Move the first node to be the last node. LinkedListNode mark1 = sentence.First; sentence.RemoveFirst(); sentence.AddLast(mark1); Display(sentence, "Test 2: Move first node to be last node:");

// Change the last node be 'yesterday'. sentence.RemoveLast(); sentence.AddLast("yesterday"); Display(sentence, "Test 3: Change the last node to 'yesterday':");

// Move the last node to be the first node. mark1 = sentence.Last; sentence.RemoveLast(); sentence.AddFirst(mark1); Display(sentence, "Test 4: Move last node to be first node:");

// Indicate, by using parentheisis, the last occurence of 'the'. sentence.RemoveFirst(); LinkedListNode current = sentence.FindLast("the"); IndicateNode(current, "Test 5: Indicate last occurence of 'the':");

// Add 'lazy' and 'old' after 'the' (the LinkedListNode named current). sentence.AddAfter(current, "old"); sentence.AddAfter(current, "lazy"); IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");

// Indicate 'fox' node. current = sentence.Find("fox"); IndicateNode(current, "Test 7: Indicate the 'fox' node:");

// Add 'quick' and 'brown' before 'fox': sentence.AddBefore(current, "quick"); sentence.AddBefore(current, "brown"); IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':");

// Keep a reference to the current node, 'fox', // and to the previous node in the list. Indicate the 'dog' node. mark1 = current; LinkedListNode mark2 = current.Previous; current = sentence.Find("dog"); IndicateNode(current, "Test 9: Indicate the 'dog' node:");

// The AddBefore method throws an InvalidOperationException // if you try to add a node that already belongs to a list. Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:"); try { sentence.AddBefore(current, mark1); } catch (InvalidOperationException ex) { Console.WriteLine("Exception message: {0}", ex.Message); } Console.WriteLine();

// Remove the node referred to by mark1, and then add it // before the node referred to by current. // Indicate the node referred to by current. sentence.Remove(mark1); sentence.AddBefore(current, mark1); IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):");

// Remove the node referred to by current. sentence.Remove(current); IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:");

// Add the node after the node referred to by mark2. sentence.AddAfter(mark2, current); IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):");

// The Remove method finds and removes the // first node that that has the specified value. sentence.Remove("old"); Display(sentence, "Test 14: Remove node that has the value 'old':");

// When the linked list is cast to ICollection(Of String), // the Add method adds a node to the end of the list. sentence.RemoveLast(); ICollection icoll = sentence; icoll.Add("rhinoceros"); Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':");

Console.WriteLine("Test 16: Copy the list to an array:"); // Create an array with the same number of // elements as the inked list. string[] sArray = new string[sentence.Count]; sentence.CopyTo(sArray, 0);

foreach (string s in sArray) { Console.WriteLine(s); }

// Release all the nodes. sentence.Clear();

Console.WriteLine(); Console.WriteLine("Test 17: Clear linked list. Contains 'jumped' = {0}", sentence.Contains("jumped"));

Console.ReadLine(); }

private static void Display(LinkedList words, string test) { Console.WriteLine(test); foreach (string word in words) { Console.Write(word + " "); } Console.WriteLine(); Console.WriteLine(); }

private static void IndicateNode(LinkedListNode node, string test) { Console.WriteLine(test); if (node.List == null) { Console.WriteLine("Node '{0}' is not in the list. ", node.Value); return; }

StringBuilder result = new StringBuilder("(" + node.Value + ")"); LinkedListNode nodeP = node.Previous;

while (nodeP != null) { result.Insert(0, nodeP.Value + " "); nodeP = nodeP.Previous; }

node = node.Next; while (node != null) { result.Append(" " + node.Value); node = node.Next; }

Console.WriteLine(result); Console.WriteLine(); }

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions