C \ # 순서 표 와 단일 체인 표 실현

6705 단어
이번 데이터 구조의 예 는 순서 표 와 단일 체인 표를 포함한다.디 버 깅 이 완료 되 었 습 니 다.
  • IListDS.cs:
  • using System;
    namespace ListDS
    {
        //      ,                 
        interface IListDS  
        {
            void Add(T item);
            T Delete(int index);
            T GetElem(int index);
            int GetLength();
            int Locate(T value);
            void Clear();
            void Insert(T item, int index);
        }
    }
    
  • SeqList.cs
  •  using System;
    namespace ListDS
    {   //   
        //  index 1  ,         
        class SeqList:IListDS
        {
            T[] data;   //      
            int count;  //      
    
            public SeqList (int length)    //      
            {
                //            ,               ,
                //                ,       
                data = new T[length];       
                count = 0;
            }
    
            public SeqList () : this (10)  //               
            {
            }
    
            public void Add (T item)
            {
                if (count == data.Length)
                    Console.WriteLine ("    ");
                else {
                    data [count] = item;
                    count++;
                }
            }
            //    index    1  
            public T Delete (int index)          
            {
                //      
                T temp = data [index-1];  
                //index-1            ,        ,    
                for (int i = index-1; i < count - 1; i++) {
                    data [i] = data [i + 1];
                }
                count--;
                return temp;
            }
    
            public T GetElem (int index)
            {
                if (index > 0 && index <= count)
                    return data [index-1];
                else {
                    Console.WriteLine ("   ");
                    return default(T);
                }
            }
    
            public int GetLength ()
            {
                return count;
            }
    
            public int Locate (T value)
            {
                for (int i = 0; i < count; i++) {
                    if (data [i].Equals (value)) {
                        return i+1;    //   index     
                    }
                }
                return -1;
            }
    
            public void Clear ()
            {
                count = 0;
            }
    
            public void Insert (T item, int index)
            {
                if (index > count)
                    Console.WriteLine ("  ");
                else {
                    count++;   //     ,            
    
                    //count 0                 count-1-1
                    for (int i = count-2; i >=index-1; i--)  
                        data[i + 1] = data[i];       //    
                    data[index-1] = item;
                }
            }
        }
    }
    
  • Node.cs
  •  using System;
    namespace ListDS
    {  //         Node
        public class Node
        {
            T data;         //  
            Node next;   //   
    
            public Node(){
                data = default(T);
                next = null;
            }
            public Node(T value){
                data = value;
                next = null;
            }
            public T Data {
                get{ return data;}
                set{ data = value;}
            }
            public Node Next {
                get{ return next;}
                set{ next = value;}
            }
        }
    }
    
  • LinkList.cs
  •  using System;
    namespace ListDS
    {
        //   
        //  index   1  ,         .
        class LinkList:IListDS
        {
            //     ,          
            Node head;
    
            public LinkList ()
            {
                head = null;
            }
    
            public void Add (T item)
            {
                //    
                Node newNode = new Node (item); 
                //       ,          
                if (head == null)
                    head = newNode;
                else {
                    //          
                    Node temp = head;
                    //    
                    while (temp.Next != null)
                        temp = temp.Next;
                    //        
                    temp.Next = newNode;
                }
            }
    
            public T Delete (int index)
            { 
                T record = default(T);
                if (head == null)
                    Console.WriteLine ("    ");
                //     
                else if (index == 1) {
                    record = head.Data;
                    head = head.Next;
                }
                else {
                    Node temp = head;
                    int count = 1;
                    //           
                    while (temp.Next != null) {
                        count++;
                        temp = temp.Next;
                    }
                    if (count < index)
                        Console.WriteLine ("         ");
                    else {
                        //   temp         ,                  
                        temp = head;
                        //               
                        for (int i = 1; i < index-1; i++)
                            temp = temp.Next;
                        //       
                        record = temp.Next.Data;
                        //      
                        temp.Next = temp.Next.Next;
                    }
                }
                return record;
            }
            //    index   ,        
            public T GetElem (int index)
            {
                Node temp = head;
                for (int i = 1; i < index; i++)
                    temp = temp.Next;
                return temp.Data;
            }
    
            public int GetLength ()
            {
                Node temp = head;
                int count = 1;
                while (temp.Next != null) {
                    count++;
                    temp = temp.Next;
                }
                return count;
            }
    
            public int Locate (T value)
            {
                Node temp = head;
                int index = 1;
                while (temp.Next != null) {
                    if (!temp.Data.Equals (value)) {
                        index++;
                        temp = temp.Next;
                    } else
                        break;
                }
                return index;
            }
    
            public void Clear ()
            {
                head = null;
                //            
            }
    
            public void Insert (T item, int index)
            {
                //     
                Node newNode = new Node (item);
    
                if (head == null) {
                    head = newNode;
                } else {
                    //      
                    if (index < 1)
                        Console.WriteLine ("    ,    ");
                    else {
                        Node temp = head;
                        //            
                        for(int i=1 ;i

    좋은 웹페이지 즐겨찾기