//学期末了,用C#演练一下 数据结构的实例
//初学,可能有幼稚的地方,大家不要笑

//要改进的地方
//1.顺序表的第0个元素可以不用它
//这样可以保持索引和数目的一致
//2.增删改插操作可以增加返回值
//用来判断操作的成功 失败
//3.可以定义 error 枚举
//返回值 返回枚举类型
//再建一个类 判断错误类型

//如果有错误 请前辈们多指教
using System;
using System.Collections.Generic;

namespace HEHE
{
    
    
class Seq_List//顺序表
    {
        
//字段
        private int _capacity=20;
        
private int _count=0;
        
public int[] list;
        
        
public Seq_List(int cap)
        {
            _capacity
=cap;
            list
=new int[_capacity];
        }
        
        
public Seq_List()
        {
            list
=new int[_capacity];
        }
        
        
public int capacity
        {
            
get
            {
                
return _capacity;
            }
            
set
            {
                _capacity
=value;
            }
        }
        
public int count
        {
            
get
            {
                
return _count;
            }
            
set
            {
                _count
=value;
            }
        }
        
        
//类方法
        
        
        
//判空
        public bool isEmpty()
        {
            
return  _count==0;
        }
        
        
//求表长
        public int length()
        {
            
return _count;
        }
        
        
//初始化
        public void InitList()
        {
            Console.WriteLine(
"下面要进行初始化,数组的容量是"+_capacity);
            Console.WriteLine(
"请输入你要初始化数组的尺寸");
            
int size=Convert.ToInt32(Console.ReadLine()); 
            
if(size>_capacity)
            {
                Console.WriteLine(
"尺寸超出最大容量");
            }
            
else
            {
                
for(int i=0;i<size;i++)
                {
                    Console.WriteLine(
"下面给list[{0}]初始化",i);
                    list[i]
=Convert.ToInt32(Console.ReadLine());
                }
                _count
=size;
                Console.WriteLine(
"初始化完毕");
            }
        }
        
//展示表
        public void ShowList()
        {
            
for(int i=0;i<_count;i++)
            {
                Console.WriteLine(
"list[{0}]={1}",i,list[i]);
            }
            Console.WriteLine(
"SHOW 完毕");
        }
        
        
//在表的末尾增加元素
        public void Add(int element)
        {
            
if(_count+1==_capacity)
            {
                Console.WriteLine(
"表满,加入失败");
            }
            
else
            {
                _count
++;
                
this.list[_count-1]=element;
            }

        }
        
public int GetElm(int index)
        {
            
if(index>_count-1)
            {
                Console.WriteLine(
"索引超出范围");
                
return -1;
            }
            
else
            {
                
return list[index];
            }
        }
        
public int exist(int element)
        {
            
int i=0;
            
for(;i<_count;i++)
            {
                
if(element==list[i])
                {
                    
return i;
                }
                i
++;
            }
            
return -1;
        }
        
public bool Update(int index,int newValue)
        {
            
if(index>_count)
            {
                
return false;
            }
            list[index]
=newValue;
            
return true;
        }
        
public bool insert(int index,int insertValue)
        {
            
if(index>_count||_count==_capacity)
            {
                
return false;
            }
            _count
++;
            
for(int i=_count;i!=index;i--)
            {
                list[i]
=list[i-1];
            }
            list[index]
=insertValue;
            
return true;
        }
        
public bool delete(int index)
        {
            
if(index>_count)
            {
                
return false;
            }
            
for(int i=index;i!=_count;i++)
            {
                list[i]
=list[i+1];
            }
            count
--;
            
return true;
        }
        
        
public bool delete(int index,int num)
        {
            
if(index+num>_count)
            {
                
return false;
            }
            
for(int i=index;i!=_count;i++)
            {
                list[i]
=list[i+num];
            }
            count
-=num;
            
return true;
        }
        
        
        
    }
    
class TestIt
    {
        
public static void Main(string[] args)
        {
            Console.WriteLine(
"实例化顺序表");
            Seq_List myList
=new Seq_List();
            Console.WriteLine(
"初始化表");
            myList.InitList();
            myList.ShowList();
            Console.WriteLine(
"Add()方法");
            myList.Add(
10);
            myList.ShowList();
            Console.WriteLine(
"求表长");
            Console.WriteLine(myList.length());
            Console.WriteLine(
"索引求元素GetElm(3)");
            Console.WriteLine(myList.GetElm(
3));
            
            Console.WriteLine(
"是否存在元素exist(3)");
            
if(myList.exist(3)!=-1)
            {
                Console.WriteLine(myList.exist(
3));
            }
            
else
            {
                Console.WriteLine(
"不存在");
            }
            
            
if(myList.Update(4,99))
            {
                Console.WriteLine(
"更新成功");
                Console.WriteLine(
"List[4]={0}",99);
            }
            
else
            {
                Console.WriteLine(
"更新失败");
            }
            
            Console.WriteLine(
"插入操作insert(2,555)");
            
if(myList.insert(2,555))
            {
                Console.WriteLine(
"插入成功");
                myList.ShowList();
            }
            
else
            {
                Console.WriteLine(
"插入失败");
            }
            
            Console.WriteLine(
"删除操作delete(3)");
            myList.delete(
3);
            myList.ShowList();
            
            Console.WriteLine(
"删除操作delete(3,2)");
            myList.delete(
3,2);
            myList.ShowList();
            Console.ReadKey();
        }
    }
}
posted on 2008-06-04 14:10  LMT  阅读(315)  评论(0)    收藏  举报