递归输出单链表,C#小程序

单链表节点:

namespace linkedlistrec.Link{
    public class Node<T> {
        public T data;
        public Node<T> next;

        public Node(T data, Node<T> next = null){
            this.data = data;
            this.next = next;
        }
    }
}

单链表类:

using System;

namespace linkedlistrec.Link {
    public class LinkedList {
        public Node<int> head;

        public LinkedList(int n){
            head = null;
            if(n>0){
                int i = 1;
                Node<int> rear,temp;
                head = new Node<int>(i++);
                rear = head;

                while(i<=n){
                    temp = new Node<int>(i++);
                    rear.next = temp;
                    rear = temp;
                }
            }
        }

        public bool isEmpty() {
            return head == null;
        }

        public bool isFull() {
            return false;
        }


        public int Length() {
            int i = 0;
            Node<int> temp = head;
            while(temp != null){
                i++;
                temp = temp.next;
            }
            return i;
        }

        public Node<int> index(int i) {
            if(i<=0) {
                return null;
            }

            int j = 0;
            Node<int>  temp = head;
            while(temp!=null && j<i) {
                j++;
                temp = temp.next;
            }

            return temp;
        }

        public int get(int i) {
            Node<int> temp = index(i);
            if(temp!=null){
                return temp.data;
            }else {
                return -32768;
            }
        }

        public bool set(int i, int k){
            Node<int> temp = index(i);
            if(temp!=null){
                temp.data = k;
                return true;
            }

            return false;
        }


        public void output(Node<int> p) {
            
            while(p!=null){
                Console.Write(p.data+" ");
                p = p.next;
            }

            Console.WriteLine();
        }


        public void output(){
            outputrec(head);
        }


        public void outputrec(Node<int> p) {
            if(p!=null){
                Console.Write(p.data+ " ");
                outputrec(p.next);
            }else{
                Console.WriteLine();
            }
            
        }

    }
}

主程序:

using System;
using linkedlistrec.Link;

namespace linkedlistrec
{
    class Program
    {
        static void Main(string[] args)
        {
           LinkedList lList = new LinkedList(9);
           lList.output();
        }
    }
}

程序输出:


1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近在复习数据结构时,感触颇深。 推荐程序员们有时间都可以复习下, 数据结构不仅仅是一门课程, 它更能理清我们开发...
    Bobby0322阅读 3,126评论 0 4
  • 作为一个资深的新手程序员😂,链表这些既基础又深奥的东西是日常工作中并不常见,但是却非常重要,所以就总结一下链表的简...
    Clark_new阅读 4,290评论 4 12
  • 今天看了得到里面的一篇文章,是讲加州大学中国研究中心主任,文化人类学家阎云翔提出的一个新的概念“新家庭主义”,什么...
    惠影风华阅读 317评论 0 0
  • 盼望着,盼望着,365极限挑战训练营终于迎来额开营的日子。 人到中年,经历的事情一多,已经很少有什么东西能够让我激...
    蓝色牛仔阅读 459评论 13 5
  • Java socket应用—通信 TCP/IP协议相关及简介: ▲TCP/IP是目前世界上应用最为广泛的协议。是以...
    少冰三hun甜阅读 363评论 0 0