How to implement user inputted integers in a sorted linked list?

Viewed 27

As stated in in the title, I would like to know how to allow the user to push the integers and print them. I have written another simple program where the user can press keys to do a few things, like push/pop/print, would it be possible to merge them even if the one is just an ordinary stack and another is a linked list?

This is is the first program that simply lets the user add a string, push/pop/print:

 class Program
    {
        static void Main(string[] args)
        {
         Stack<string> mystack = new Stack <string>();
         int number = -1;
         while (number != 0)
            {
                Console.WriteLine("1- enter string");
                Console.WriteLine("2 - delete string");
                Console.WriteLine("3- print all strings");
                Console.WriteLine("0- Exit");
                number = Convert.ToInt32(Console.ReadLine());
                {
             switch (number)
                {
                   case 1:
                        Console.Write("Enter string: ");
                        mystack.Push(Console.ReadLine());


                        break;
                    case 2:
                        mystack.Pop();
                        Console.WriteLine("first string deleted");


                        break;
                    case 3:
                        foreach (string i in mystack)
                        {
                            Console.WriteLine(i);
                        }


                            

                            break;
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

This is the second which is a linked list sorted in ascending order but the integers are inputted by the coder.

{
    class LinkedList
    {
        public class node
        {
            public int data;
            public node next;
        };
        static node start;

        static void sortList(node head)
        {
            int startVal = 1;

            while (head != null)
            {
                head.data = startVal;
                startVal++;
                head = head.next;
            }
        }
        static void push(node head_ref,
                         int new_data)
        {
            
            node new_node = new node();
            new_node.data = new_data;
            new_node.next = head_ref;
            head_ref = new_node;
            start = head_ref;
        }

        static void printList(node node)
        {
            while (node != null)
            {
                Console.Write(node.data + " ");

                node = node.next;
            }
        }

        public static void Main(String[] args)
        {
            start = null;
            push(start, 2); 
            push(start, 1); 
            push(start, 6); 
            push(start, 4); 
            push(start, 5); 
            push(start, 3); 
    
                    sortList(start);

                    printList(start);

                    Console.ReadKey();
            }
1 Answers

You are trying to merge the functionality of two different data types that have different usage.

Stack and Linked List are two linear data structures. A programmer can implement them using any programming language. The main difference between Stack and Linked List is that a Stack works according to the FIFO mechanism while a Linked List works by storing the data and the addresses of other nodes to refer to each other.

In a stack, the topmost element can be read. On the other hand, in a linked list, if the programmer wants to access a specific element, it is necessary to traverse each element from the beginning.

Though you can use the combination of both in such a way to push the data to stack in a sorted manner.

Reference: https://pediaa.com/what-is-the-difference-between-stack-and-linked-list/#:~:text=A%20stack%20is%20an%20abstract,between%20stack%20and%20linked%20list.

Related