【问题标题】:How to implement user inputted integers in a sorted linked list?如何在排序链表中实现用户输入的整数?
【发布时间】:2020-11-15 06:33:36
【问题描述】:

如标题中所述,我想知道如何允许用户推送整数并打印它们。我写了另一个简单的程序,用户可以按键来做一些事情,比如推/弹出/打印,即使一个只是普通堆栈而另一个是链表,是否可以合并它们?

这是第一个简单地让用户添加字符串的程序,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();
        }
    }
}

这是第二个,它是一个按升序排序的链表,但整数是由编码器输入的。

{
    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();
            }

【问题讨论】:

    标签: c# linked-list stack implementation


    【解决方案1】:

    您正在尝试合并具有不同用途的两种不同数据类型的功能。

    堆栈和链表是两种线性数据结构。程序员可以使用任何编程语言来实现它们。 Stack 和 Linked List 的主要区别在于 Stack 是按照 FIFO 机制工作的,而 Linked List 是通过存储数据和其他节点的地址来相互引用的。

    在堆栈中,可以读取最顶部的元素。另一方面,在链表中,如果程序员想要访问特定的元素,则需要从头开始遍历每个元素。

    尽管您可以将两者结合使用,从而以排序方式将数据推送到堆栈。

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

    【讨论】:

    • 谢谢,这对我为什么遇到麻烦很有意义,知道如何获得堆栈程序的功能(我的意思是用户输入部分)。在链接列表中?
    • 我再说一遍你正在尝试混合
    • 所以当你将项目推入堆栈时,你可以以排序的方式推入,这样你的堆栈就会被排序
    猜你喜欢
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2021-08-24
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多