顺序存储线性表

  1 // Stroed in order list complete
  2 
  3 #include <iostream>
  4 
  5 const int MAXSIZE=100;
  6 //list
  7 struct List
  8 {
  9     int data[MAXSIZE];
 10     int length;
 11 };
 12 bool InitList(List *l)
 13 {
 14     l->length=0;
 15     return true;
 16 }
 17 bool ListEmpty(List l)
 18 {
 19     if(l.length==0)
 20         return true;
 21     return false;
 22 }
 23 int GetLength(List l)
 24 {
 25     return l.length;
 26 }
 27 
 28 bool ClearList(List *l)
 29 {
 30     l->length = 0;
 31     return true;
 32 }
 33 // return the number which index is i in List List.  
 34 int GetElem(List l,int index)
 35 {
 36     if(index<0 || index>l.length || l.length==0) //  add l.length ==0  
 37     {
 38         return -1;
 39     }
 40     return l.data[index-1];
 41 }
 42 // return the number's index
 43 int GetIndex(List l,int number)
 44 {
 45     if(l.length==0)
 46         return -1;
 47     int index;
 48     for(index=0;index<l.length;index++)
 49     {
 50         if(l.data[index]==number)
 51             break;
 52     }
 53     if(index>=l.length)          // lost 
 54         return -1; 
 55     return index+1;
 56 }
 57 // insert a number int List at index
 58 bool ListInsert(List *l,int index,int num)
 59 {
 60     if(l->length==MAXSIZE)
 61         return false;
 62     if(index<1||index>l->length+1)
 63         return false;
 64     int k;
 65     if(index<=l->length)
 66     {
 67         for(k=l->length-1;k>=index-1;k--)
 68         {
 69             l->data[k+1]=l->data[k];
 70         }
 71     }
 72     l->data[index-1]=num;
 73     l->length++;
 74     return true;
 75 }
 76 //delete the number at index
 77 bool ListDelete(List *l,int index)
 78 {
 79     if(l->length==0)
 80         return false;
 81     if(index<1||index>l->length)
 82         return false;
 83     int k;
 84     if(index<l->length)
 85     {
 86         for(k=index-1;k<l->length-1;k++)
 87         {
 88             l->data[k]=l->data[k+1];
 89         }
 90     }
 91     l->length--;
 92     return true;
 93 }
 94 
 95 void Listprintf(List l)
 96 {
 97     using namespace std;
 98     if(l.length==0)
 99         cout<<"ZERO!";
100     int index;
101     for(index=0;index<l.length;index++)
102     {
103         cout<<"List["<<index<<"]"<<l.data[index]<<endl;
104     }
105 }
106 
107 // union two to one
108 bool Listunion(List *one,List two)
109 {
110     using namespace std;
111     int one_len,two_len;
112     one_len=GetLength(*one);
113     two_len=GetLength(two);
114     int i;
115     int temp;
116     for(i=1;i<=two_len;i++)
117     {
118         temp=GetElem(two,i);
119         if(GetIndex(*one,temp)==-1)
120         {
121             if(ListInsert(one,++one_len,temp))
122             {
123                 cout<<temp<<"插入成功。"<<endl; 
124             }
125             else
126             {
127                 cout<<temp<<"插入失败。"<<endl;
128                 return false;
129             }
130         }
131     }
132 }
133 
134 int main()
135 {
136     using namespace std;
137     List L;
138     if(InitList(&L))
139         cout<<"L InitSucess."<<endl;
140     for(int i=0;i<6;i++)
141         ListInsert(&L,1,i);
142     Listprintf(L);
143     cout<<(ListEmpty(L)==false?"非空":"空表")<<endl;
144     ListInsert(&L,2,1000);
145     Listprintf(L);
146 //    ClearList(&L);
147 //    cout<<(ListEmpty(L)==false?"非空":"空表")<<endl;
148 //    Listprintf(L);
149     ListDelete(&L,2);
150     cout<<"删除第二个元素"<<endl;
151     Listprintf(L);
152     cout<<"此表的最后一个元素为:"<<GetElem(L,L.length)<<endl;
153     List L2;
154     if(InitList(&L2))
155         cout<<"L2 InitSucess."<<endl;
156     cout<<"向L2位置一中插入5,100,99,0,33,12"<<endl; 
157     ListInsert(&L2,1,5);
158     ListInsert(&L2,1,100);
159     ListInsert(&L2,1,99);
160     ListInsert(&L2,1,0);
161     ListInsert(&L2,1,33);
162     ListInsert(&L2,1,12);
163     Listprintf(L2);
164     cout<<"把L2合并到L1"<<endl;
165     Listunion(&L,L2);
166     Listprintf(L);
167     return 0;
168 }

经过一个中午的进行,最终按自己的理解写出来了一个顺序存储线性表,哇,有点开心。嘿嘿!赶快记下来。

在调试中发现了* 和 & 啥时候带指针参数啥时候不带,不带又会有啥后果,我刚开始初始化的时候没用带指针的参数试了一下,结果崩溃了,嘿嘿,因为那数根本 没改变为计划的数值0,而是一个超级大数。。。如果想插入,也必须用指针奥,如果没用执行后发现,这个表里啥都没插进去。哦。。。希望自己以后回来看的时候,能发现好多问题。恩!

转载于:https://www.cnblogs.com/mengjiangtao/archive/2013/03/02/2940070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值