单链表(C#)实现

 前几天的Homework。

 

using  System;

public   interface  list
    
{
        
bool IsEmpty();
        
object CreatList(int n);
        
void Insert(int index,object item);
        
bool Delete(object item);
        
bool Search(object item);
        
void PrintAll();
        
void Clear();
    }


    
class  SNode
    
{
     
...
    }


    
public   class  SList : list
    
{
        
private SNode head;
        
private int length;

        
public int number
        
{
            
get
            
{
                
return length;
            }

            
set
            
{
                length 
= value;
            }

        }


        
public SList()
        
{
            
this.head = null;
        }


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


        
public object CreatList(int n)
        
{
            Console.WriteLine(
"please input {0} items:",n);
            
for (int i = 0; i < n; i++)
            
{
                
object temp = Console.ReadLine();
                Insert(i,temp);
            }

            
return head;
        }


        
public void PrintAll()
        
{
            Console.Write(
"the SList has {0} items:",length);
            
string result = null;
            
for (SNode temp = head; temp != null; temp = temp.next)
            
{
                result 
+= temp.value.ToString() + " ";
            }

            Console.WriteLine(result);
        }
// end of PrintAll

        
public bool Search(object item)
        
{
            
if (head == null)
                
return false;
            
else
            
{
                
for (SNode temp = head; temp != null; temp = temp.next)
                
{
                    
if (temp.value.Equals(item))
                        
return true;
                }

            }

            
return false;
        }
 // end of search

        
public bool Delete(object item)
        
{
            SNode pre, temp;
            
// if the SList is empty
            if (IsEmpty())
                
return false;

            
//if delete the head
            if (head.value.Equals(item))
            
{
                head 
= head.next;
                
this.length--;
            }

            
else
            
{
                
for (pre = head, temp = head.next; temp != null; pre = pre.next, temp = temp.next)
                
{
                    
if (temp.value.Equals(item))
                    
{
                        pre.next 
= temp.next;
                        temp 
= null;
                        
this.length--;
                        
break;
                    }

                }


                
// to judg whether find the item or loop end 
                if (pre.next == null)
                    
return false;
                
else
                    
return true;
            }
// end of else
            return true;
        }


        
public void Insert(int index, object item)
        
{
            
int i;
            SNode temp;
            
while (index < 0 || index > length)
            
{
                Console.WriteLine(
"the index overflow");
                Console.WriteLine(
"please input the correct index from 0 to {0}:", length);
                index 
= Convert.ToInt32(Console.ReadLine());
            }

            
// if insert befor the head
            if (index == 0)
            
{
                temp 
= new SNode(item);
                temp.next 
= head;
                head 
= temp;
                length
++;
            }

            
else
            
{
                
// insert in the middle of SList
                SNode p = head;
                
for (i = 1; i < index; i++)
                
{
                    p 
= p.next;
                }

                temp 
= new SNode(item);
                temp.next 
= p.next;
                p.next 
= temp;
                length
++;
            }

        }


        
public void Clear()
        
{
            
while (!IsEmpty())
            
{
             Console.WriteLine(
"delete "{0}" from the SList",head.value);
                Delete(head.value);            
            }

            
if ( length  == 0 )
             Console.WriteLine(
"the SList is empty");
        }

    }
//  end of SList class

    
public   class  MainEntry
    
{
        
static void Main()
        
{

            SList list1 
= new SList();
            Console.WriteLine(
"please input the number of the list that you want to create: ");
            
int n = Convert.ToInt32(Console.ReadLine());
            
//list1.number = n;
            
            
// test create the SList
            Console.Write(" ************************************************************** ");
            Console.WriteLine(
"Creat SList");
            list1.CreatList(n);
            list1.PrintAll();
            Console.WriteLine(
"**************************************************************");

            
// test delete one item
            Console.Write(" ************************************************************** ");
            Console.WriteLine(
"Remove item from the SList");
            Console.WriteLine(
"please input the item you want to delete:");
            
object obj = Console.ReadLine();
            
if (list1.Delete(obj))
                Console.WriteLine(
"has delete the item {0}",obj);
            
else
                Console.WriteLine(
"SList hasnot contain the item {0}",obj);
            list1.PrintAll();
            Console.WriteLine(
"**************************************************************");

            
// test search one item in the SList
            Console.Write(" ************************************************************** ");
            Console.WriteLine(
"Search item in the SList");
            Console.WriteLine(
"please input the item you want to search:");
            obj 
= Console.ReadLine();
            
if (list1.Search(obj))
                Console.WriteLine(
" has find the item {0}", obj);
            
else Console.WriteLine(" SList doesn't contain the item {0}", obj);
            Console.WriteLine(
"**************************************************************");

            
//test insert item in given position in the SList
            Console.Write(" ************************************************************** ");
            Console.WriteLine(
"insert function start");
            Console.WriteLine(
"input paraments like this------index:xxx,item:xxx");
            Console.WriteLine(
"the index present the position after which you want to insert");
            Console.WriteLine(
"please input the index from 0 to {0}:",list1.number);
            
int index =Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(
"please input the item:");
            obj 
= Console.ReadLine();
            list1.Insert(index,obj);
            list1.PrintAll();
            Console.WriteLine(
"**************************************************************");

            
// clear the SList
            Console.Write(" ************************************************************** ");
            Console.WriteLine(
"Clear SList");
            list1.Clear();
            Console.WriteLine(
"**************************************************************");
            Console.ReadKey();
        }

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值