简体   繁体   English

如何在排序链表中实现用户输入的整数?

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

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

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. Stack 和 Linked List 的主要区别在于 Stack 根据 FIFO 机制工作,而 Linked List 的工作方式是存储数据和其他节点的地址以相互引用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM