Question

what are append, pre-append and display current state of the list method in linkedList C#. Please...

what are append, pre-append and display current state of the list method in linkedList C#. Please explain with examples

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

What is LinkedList?

A LinkedList is a linear data structure which stores element in the non-contiguous location. The elements in a linked list are linked with each other using pointers. In C#, LinkedList is the generic type of collection which is defined in System.Collections.Generic namespace. It is a doubly linked list, therefore, each node points forward to the Next node and backward to the Previous node. It is a dynamic collection which grows, according to the need of your program. It also provides fast inserting and removing elements.

Constructors

A LinkedList class has 3 constructors which are used to create a LinkedList which are as follows:

  1. LinkedList(): This constructor is used to create an instance of the LinkedList class that is empty.
  2. LinkedList(IEnumerable): This constructor is used to create an instance of the LinkedList class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied.
  3. LinkedList(SerializationInfo, StreamingContext): This constructor is used to create an instance of the LinkedList class that is serializable with the specified SerializationInfo and StreamingContext

Methods

  • Append:Append adds data to a StringBuilder. Append method calls can be chained. This makes StringBuilder code more succinct and easier to read.StringBuilder
  • AppendLine, the Framework places a newline sequence at the end of the specified string. This method makes code easier to read as well.

For example:

StringBuilder pp=new StringBuilder();

pp.AppendLine("Demo");

pp.Insert(0,Environment.NewLine);

  • Prepend Method is used to add one value to the beginning of a sequence. This Prepend method like the Append method does not modify the elements of the sequence. Instead, it creates a copy of the sequence with the new element.

For example:

StringBuilder pp=new StringBuilder();

pp.Appendline("Demo1");

pp.Prepend(Environmet.NewLine);

  • 4 different methods to add nodes
  1. AddAfter: This method is used to add a new node or value after an existing node in the LinkedList.
  2. AddBefore: This method is used to add a new node or value before an existing node in the LinkedList.
  3. AddFirst() method insert the specified element at the beginning of the linked list
  4. AddLast() method insert the specified element at the end of the linked list.
  • 5 different methods to remove elements
  1. Clear(): This method is used to remove all nodes from the LinkedList.
  2. Remove(LinkedListNode): This method is used to remove the specified node from the LinkedList.
  3. Remove(T): This method is used to remove the first occurrence of the specified value from the LinkedList.
  4. RemoveFirst(): This method is used to remove the node at the start of the LinkedList.
  5. RemoveLast(): This method is used to remove the node at the end of the LinkedList.
  • Contains(T):This method is used to determine whether a value is in the LinkedList.

Example Program1:

The LinkedList is accessed by using a foreach loop or by using for loop.

This program show how to create a LinkedList

using System;

using System.Collections.Generic;

class FT {

    // Main Method

    static public void Main()

    {

        // Creating a linkedlist

        // Using LinkedList class

        LinkedList<String> my_list = new LinkedList<String>();

        // Adding elements in the LinkedList

        // Using AddLast() method

        my_list.AddLast("Apple");

        my_list.AddLast("Orange");

        my_list.AddLast("Pineapple");

        my_list.AddLast("Jackfruit");

        my_list.AddLast("Graphes");

        my_list.AddLast("Apple");

        Console.WriteLine("Best fruits for the health:");

        // Accessing the elements of

        // LinkedLis​​​​​​​t Using foreach loop

        foreach(string str in my_list)

        {

            Console.WriteLine(str);

        }

    }

}

Output

 Best fruits for the health: Apple Orange Pineapple Jackfruit Graphes Apple

Example Program2:

Program to illustrate how to remove elements from Linkedlist

using System;

using System.Collections.Generic;

class FT {

    // Main Method

    static public void Main()

    {

        // Creating a linkedlist

        // Using LinkedList class

        LinkedList<String> my_list = new LinkedList<String>();

        // Adding elements in the LinkedList

        // Using AddLast() method

        my_list.AddLast("Apple");

        my_list.AddLast("Orange");

        my_list.AddLast("Pineapple");

        my_list.AddLast("Jackfruit");

        my_list.AddLast("Graphes");

        my_list.AddLast(" Apple");

        // Inital number of elements

        Console.WriteLine("Best fruits for the health "+

                         "initially:");

        // Accessing the elements of

        // Linkedlist Using foreach loop

        foreach(string str in my_list)

        {

            Console.WriteLine(str);

        }

        // After using Remove(LinkedListNode)

        // method

        Console.WriteLine("Best fruits for the health"+

                         " in 2016:");

        my_list.Remove(my_list.First);

        foreach(string str in my_list)

        {

            Console.WriteLine(str);

        }

        // After using Remove(T) method

        Console.WriteLine("Best fruits for the health"+

                         " in 2017:");

        my_list.Remove("Rohit");

        foreach(string str in my_list)

        {

            Console.WriteLine(str);

        }

        // After using RemoveFirst() method

        Console.WriteLine("Best fruits for the health"+

                         " in 2018:");

        my_list.RemoveFirst();

        foreach(string str in my_list)

        {

            Console.WriteLine(str);

        }

        // After using RemoveLast() method

        Console.WriteLine("Best fruits for the health"+

                         " in 2019:");

        my_list.RemoveLast();

        foreach(string str in my_list)

        {

            Console.WriteLine(str);

        }

        // After using Clear() method

        my_list.Clear();

        Console.WriteLine("Number of fruits: {0}",

                                     my_list.Count);

    }

}

Output

 Best fruits for the health initially: Apple Orange Pineapple Jackfruit Graphes Apple Best fruits for the health in 2016: Orange Pineapple Jackfruit Graphes Apple Best fruits for the health in 2017: Orange Jackfruit Graphes Apple Best fruits for the health in 2018: Jackfruit Graphes Apple Best fruits for the health in 2019: Jackfruit Graphes Number of fruits: 0 
Add a comment
Know the answer?
Add Answer to:
what are append, pre-append and display current state of the list method in linkedList C#. Please...
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
  • 1. What is the result of calling the append method on a list? 2. What must...

    1. What is the result of calling the append method on a list? 2. What must be defined prior to using a method like append? 3. Explain why two lines caused an IndexError. 4. What is the result of calling the remove method on a list? 5. Give one example of a list method that requires an argument and one that does not. 6. Describe the syntax similarities and differences between using a list method like append and Python built-in...

  • java c++ 1. Write a method called deleteFromList in an application (not in the class LinkedList)...

    java c++ 1. Write a method called deleteFromList in an application (not in the class LinkedList) with the header public static void deleteFromList (LinkedList LL, LinkedList Current) to delete all values found in linked list LL from another linked list Current. The figure below demonstrates the concept List LL 55 45 Current List 77 55 50 55 2 Current after deletion 77 50 2 2. There are many ways to detect if a phrase is a palindrome. You already learned...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • C++ Given this linked list class: class LinkedList { private: Node * head; public: LinkedList(); void...

    C++ Given this linked list class: class LinkedList { private: Node * head; public: LinkedList(); void prepend(int val); int sum(); }; Implement a sum method which will return the sum of the values stored in the linked list.

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • Python code: using only len(), range(), append(), and del keyword when needed as the directions state?...

    Python code: using only len(), range(), append(), and del keyword when needed as the directions state? No list comprehension, slicing, or importing modules. Please and thank you. def diagonal_matrix(mat) Converts a square matrix mat into a lower diagonal matrix https://en.wikipedia.org/wiki/Triangular_matrix After the conversion the values on the diagonal will be the sum of the deleted elements in the respective row Return value is None. Variable mat is directly modi ed in memory mat is a nested list (e.g. [[1,2,3], [4,5,6],...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

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