| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2806 人关注过本帖
标题:C#数据结构删除用法
只看楼主 加入收藏
冷锋2048
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2015-10-27
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:2 
C#数据结构删除用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    public class LinkedList
    {
        //成员
        private int count;//记录元素个数
        private Node head;//头指针
        //方法
        public void Add(object value)//在链表结尾添加元素
        {
            Node newNode = new Node(value);
            if (head == null)
            {//如果链表为空则直接作为头指针
                head = newNode;
            }
            else//否则插入链表结尾
            {
                GetByIndex(count - 1).next = newNode;
            }
            count++;
        }//在指定索引处插入元素
        public void Insert(int index, object value)
        {
            Node tempNode;
            if (index == 0)
            {
                if (head == null)
                {
                    head = new Node(value);
                }
                else
                {
                    tempNode = new Node(value);
                    tempNode.next = head;
                    head = tempNode;
                }
            }

            else
            {
                Node prevNode = GetByIndex(index - 1);//查找插入点的前驱结点
                Node nextNode = prevNode.next;//插入点的后继结点
                tempNode = new Node(value);//新结点
                prevNode.next = tempNode;//前驱结点的后继结点为新结点
                tempNode.next = nextNode;//指定新结点的后继结点
            }
            count++;
        }
        public void RemoveAt(int index)//删除指定索引元素
        {
            if (index == 0)          //如果要删除开始结点
            {
                head = head.next;
            }
            else
            {
                Node prevNode = GetByIndex(index - 1);//查找删除结点的前驱结点
                if (prevNode.next == null)
                {
                    throw new ArgumentOutOfRangeException("index", "索引超出范围");
                }
                prevNode.next = prevNode.next.next;//删除
            }
            count--;
        }
        public override string ToString()         //打印整个链表,仅用于测试
        {
            string s = "";
            for (Node temp = head; temp != null; temp = temp.next)
            {
                s += temp.ToString() + "";
            }
            return s;
        }
        private Node GetByIndex(int index)    //查找指定索引的元素
        {
            if ((index < 0) || (index >= this.count))
            {
                throw new ArgumentOutOfRangeException("index", "索引超出范围");
            }
            Node tempNode = this.head;
            for (int i = 0; i < index; i++)
            {
                tempNode = tempNode.next;
            }
            return tempNode;
        }
        //删除所选元素的方法
        public void Remove(object value)
        {
            Node NowNode = new Node(value);
            int j = 0;
            Node tempNode = this.head;
            for (int i = 0; i < count; i++)
            {
                if (NowNode.item == tempNode.item)
                {
                    break;
                }
                else
                {
                    tempNode = tempNode.next;
                    j++;
                }

            }
            RemoveAt(j);
        }      
        //属性
        public int Count //指示链中的元素个数
        {
            get { return count; }
        }
        public object this[int index]
        {
            get { return GetByIndex(index).item; }
            set { GetByIndex(index).item = value; }
        }
        //嵌套类,表示单个结点
        private class Node
        {
            public Node(object value)
            {
                item = value;
            }
            public object item;//数据域
            public LinkedList.Node next;//指针域,指向后继结点
            public override string ToString()
            {
                return item.ToString();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LinkedList lst = new LinkedList();
            lst.Add(0);//添加
            lst.Add(1);
            lst.Add(2);
            lst.Add(3);
            lst.Insert(2, 50);//插入
            Console.WriteLine(lst.ToString());
            lst.RemoveAt(1);//删除
            lst[2] = 9;//访问
            Console.WriteLine(lst.ToString());
            Console.WriteLine("输入要添加的元素");
            object n = Console.ReadLine();
            lst.Add(n);
            Console.WriteLine(lst.ToString());
            Console.WriteLine("输入要删除的元素");
            object m = Console.ReadLine();
            lst.Remove(m);
            Console.WriteLine(lst.ToString());
            Console.ReadKey();
        }
    }
}
我的问题是删除所选元素那个方法有问题,一直报错,我也看了很久,不知道哪里出错了,希望有人指导一下。
搜索更多相关主题的帖子: private public count null 记录 
2016-04-18 22:22
a646404908
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:189
专家分:492
注 册:2012-2-14
收藏
得分:20 
程序代码:

 public void Remove(object value)
        {
            Node NowNode = new Node(value);
            bool ischeck = false;
            int i;
            Node tempNode = this.head;
            Node shang = null;
            for (i = 0; i < count; i++)
            {
                if ((NowNode.item.ToString(), tempNode.item.ToString()) == 0)
                {
                    if (tempNode.next == null)
                    {
                        if (shang == null)
                        {
                            this.head = null;

                        }
                        else
                        {
                            shang.next = null;
                        }
                        //tempNode.next = tempNode.next.next;                  
                    }
                    else
                    {
                        if (i == 0)
                        {
                            head = tempNode.next;
                        }
                        else
                        {
                            shang.next = tempNode.next;
                        }
                    }
                    count -= 1;
                    ischeck = true;
                    break;
                }
                shang = tempNode;
                tempNode = tempNode.next;
                
            }
            if (!ischeck)
            {
                Console.WriteLine("不存在该数据");
            }
           // RemoveAt(j);
        }
2016-04-19 21:35
a646404908
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:189
专家分:492
注 册:2012-2-14
收藏
得分:0 
你直接加多个属性 ..prior。。就简单多啦。。。prior。。
写成双链表。。
2016-04-19 21:41
快速回复:C#数据结构删除用法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016270 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved