none
How do l create a function to delete the letter 'a' from words that start with letter 'a' from a LinkedList for example deleting letter 'a' from word 'apple'? RRS feed

  • Question

  • using System;
    using System.Collections;
    using System.Collections.Generic;

    namespace LinkedLists
    {
        class Program
        {
            static void Main(string[] args)
            {
                LinkedList linkedList = new LinkedList();

                linkedList.AddStart("apple");
                linkedList.AddEnd("apricot");
                linkedList.AddEnd("banana");
                linkedList.AddStart("grapes");
                linkedList.RemoveEnd();
                linkedList.Display();
                

            }
        }


       public class Node
        {
            public object data;
            public Node next;

        }

        class LinkedList
        {
            Node head;
            Node current;
            int counter = 0;

            public LinkedList()
            {
                head = new Node();
                current = head;
            }

            public void AddStart(object data)
            {
                
                Node newnode = new Node();
                newnode.next = head.next;
                newnode.data = data;
                counter++;
            }
            public void AddEnd(object data)
            {
                Node newnode = new Node();
                newnode.data = data;
                current.next = newnode;
                current = newnode;
                counter++;

            }
            public void RemoveStart()
            {
                if (head.next.ToString().Contains("a") && head.next.next.ToString().Contains("a"))
                {
                    head.next.ToString().Replace("a", " ");
                }
                if (counter > 0)
                {
                    head.next = head.next.next;
                    counter--;
                }
                else
                {
                    Console.WriteLine("No element exists in the linked list");
                    Console.ReadLine();
                }
            }
            public void RemoveEnd()
            {
                if (counter > 0)
                {
                    Node prevNode = new Node();
                    Node cur = head;
                    while (cur.next != null)
                    {
                        prevNode = cur;
                        cur = cur.next;
                    }
                    prevNode.next = null;

                    string data = cur.data.ToString();

                }
                else
                {
                    Console.WriteLine("No element exist in this linked list.");
                    Console.ReadLine();
                }
            }
            public void RemoveLetter()
            {

            }
            public void Display()
            {
                Console.Write("Head ->");
                Node curr = head;
                while (curr.next != null)
                {
                    curr = curr.next;
                    Console.WriteLine(curr.data.ToString());
                    
                }
                Console.ReadLine();
            }
        }

    }
    Thursday, April 25, 2019 11:11 AM

Answers

  • This looks like a homework assignment so I won't give you the code but I'll give you the algorithm you need to use.

    Firstly you're using object instead of string. Is there any reason why you aren't using a string here? By using object you are potentially making this code harder. To get to the "does it start with an a" part you'll have to convert each object to a string. You didn't say whether you wanted the change to be permanent but it sounds like you do want that so you'll end up having to "save" the changed value as a string which means you are now potentially dealing with mixed types which will break any code that relies on this not happening (pretty much everybody). Example:

    class MyData
    {
       public string Name { get; set; }
       public override string ToString () 
       {
          return Name;
       }
    }
    
    LinkedList linkedList = new LinkedList();
    linkedList.AddStart(new MyData() { Name = "April" });
    linkedList.AddStart(new MyData() { Name = "Bob" });
    
    linkedList.RemoveA();
    
    //linkedList now has 2 string values instead of MyData

    Nevertheless here's the algorithm.

    Foreach node in the list
       Get the data property
       Convert the data property to a string using ToString, keep in mind data could be null
       If the string result StartsWith("A") - do case insensitive here though, then 
          new trimmed value = current value Substr(1)
          Set current node's data property to new trimmed value


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 25, 2019 2:24 PM
    Moderator
  • Hi  Isheunesu Tembo,

    Thank you for posting here.

    From your description, I have made a sample on my side.

    You could try the following code to delete the letter 'a' from words that start with letter 'a'.

       class LinkedList
        {
            Node head;
            Node current;
            public LinkedList()
            {
                head = new Node();
                current = head;
            }
    
            public void AddStart(object data)
            {
                if (head.data != null)  
                {
                    Node newnode = new Node();
                    newnode.data = data;
                    newnode.next = head;
                    head = newnode;
                }
                else
                {
                    head.data = data;
                }
            }
            public void AddEnd(object data)
            {
                if (current.data != null)
                {
                    Node newnode = new Node();
                    newnode.data = data;
    
                    if (current.next == null)
                    {
                        current.next = newnode;
                        current = newnode;
                    }
                }
                else
                {
                    current.data = data;
                }
            }
            public void RemoveFirstLetter()
            {
                Node index = head;
                if (head.data == null)
                {
                    Console.WriteLine("No data");
                }
                else
                {
                    while (index.next != null)
                    {
                        if (index.data.ToString().ToLower().StartsWith("a"))
                        {
                            index.data = index.data.ToString().Substring(1);
                        }
                        index = index.next;
                    }
                }
            }
    
    
            public void DisplayWord()
            {
                Node index = head;
                if (head.data == null)
                {
                    Console.WriteLine("No data");
                }
                else
                {
                    while (index.data != null)
                    {
                        Console.WriteLine(index.data.ToString());
                        if (index.next == null)
                        {
                            break;
                        }
                        index = index.next;
                    }
                }
            }
    }

     One of the results is shown below:




    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 29, 2019 5:51 AM
    Moderator

All replies

  • This looks like a homework assignment so I won't give you the code but I'll give you the algorithm you need to use.

    Firstly you're using object instead of string. Is there any reason why you aren't using a string here? By using object you are potentially making this code harder. To get to the "does it start with an a" part you'll have to convert each object to a string. You didn't say whether you wanted the change to be permanent but it sounds like you do want that so you'll end up having to "save" the changed value as a string which means you are now potentially dealing with mixed types which will break any code that relies on this not happening (pretty much everybody). Example:

    class MyData
    {
       public string Name { get; set; }
       public override string ToString () 
       {
          return Name;
       }
    }
    
    LinkedList linkedList = new LinkedList();
    linkedList.AddStart(new MyData() { Name = "April" });
    linkedList.AddStart(new MyData() { Name = "Bob" });
    
    linkedList.RemoveA();
    
    //linkedList now has 2 string values instead of MyData

    Nevertheless here's the algorithm.

    Foreach node in the list
       Get the data property
       Convert the data property to a string using ToString, keep in mind data could be null
       If the string result StartsWith("A") - do case insensitive here though, then 
          new trimmed value = current value Substr(1)
          Set current node's data property to new trimmed value


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 25, 2019 2:24 PM
    Moderator
  • Thank you very much

    Sunday, April 28, 2019 6:00 PM
  • Hi  Isheunesu Tembo,

    Thank you for posting here.

    From your description, I have made a sample on my side.

    You could try the following code to delete the letter 'a' from words that start with letter 'a'.

       class LinkedList
        {
            Node head;
            Node current;
            public LinkedList()
            {
                head = new Node();
                current = head;
            }
    
            public void AddStart(object data)
            {
                if (head.data != null)  
                {
                    Node newnode = new Node();
                    newnode.data = data;
                    newnode.next = head;
                    head = newnode;
                }
                else
                {
                    head.data = data;
                }
            }
            public void AddEnd(object data)
            {
                if (current.data != null)
                {
                    Node newnode = new Node();
                    newnode.data = data;
    
                    if (current.next == null)
                    {
                        current.next = newnode;
                        current = newnode;
                    }
                }
                else
                {
                    current.data = data;
                }
            }
            public void RemoveFirstLetter()
            {
                Node index = head;
                if (head.data == null)
                {
                    Console.WriteLine("No data");
                }
                else
                {
                    while (index.next != null)
                    {
                        if (index.data.ToString().ToLower().StartsWith("a"))
                        {
                            index.data = index.data.ToString().Substring(1);
                        }
                        index = index.next;
                    }
                }
            }
    
    
            public void DisplayWord()
            {
                Node index = head;
                if (head.data == null)
                {
                    Console.WriteLine("No data");
                }
                else
                {
                    while (index.data != null)
                    {
                        Console.WriteLine(index.data.ToString());
                        if (index.next == null)
                        {
                            break;
                        }
                        index = index.next;
                    }
                }
            }
    }

     One of the results is shown below:




    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 29, 2019 5:51 AM
    Moderator