单链表的建立与实现 2019/7/29

  1 #include <iostream>
  2 #include <cstdlib>
  3 
  4 using namespace std;
  5 
  6 struct node{
  7     int data;
  8     node *next;
  9 };
 10 
 11 struct node *head=NULL;
 12 
 13 /*用于摧毁单链表的全部数据*/
 14 void DestoryList()
 15 {
 16     head=NULL;
 17     cout<<"Destiryed  The List!"<<endl;
 18     exit(-1);
 19 }
 20 /*用于清除单链表的数据*/
 21 void ClearList()
 22 {
 23     head=NULL;
 24     cout<<"Clear Now!"<<endl;
 25 }
 26 /*判断单链表是否为空*/
 27 bool ListEmpty()
 28 {
 29     if(head==NULL){
 30         cout<<"List is empty"<<endl;
 31         return true;
 32     }
 33     return false;
 34 }
 35 /*用于输出单链表的长度*/
 36 int ListLength()
 37 {
 38     if(head==NULL){
 39         return 0;
 40     }
 41     else{
 42         int i=0;
 43         node *p;
 44         p=head;
 45         while(p){
 46             i++;
 47             p=p->next;
 48         }
 49         return i;
 50     }
 51 }
 52 /*用于找到第i个元素,并返回其值*/
 53 int GetElem(int i)
 54 {
 55     if(i<=0){
 56         cout<<"input error"<<endl;
 57         exit(-1);
 58     }
 59     int j=1;
 60     if(ListEmpty()){
 61         cout<<"List is empty";
 62         exit(-1);
 63     }
 64     node *p;
 65     p=head;
 66     while(j!=i && p){
 67         p=p->next;
 68         j++;
 69     }
 70     if(p==NULL){
 71         exit(-1);
 72     }
 73     else{
 74         return p->data;
 75     }
 76 }
 77 
 78 /*用于向链表添加新的元素*/
 79 void AddList(node *a)
 80 {
 81     if(head==NULL){
 82         head=a;
 83         a->next=NULL;
 84     }
 85     else{
 86         node *p;
 87         p=head;
 88         while(p){
 89             if(p->next==NULL){
 90                 p->next=a;
 91                 a->next=NULL;
 92                 break;
 93             }
 94             p=p->next;
 95         }
 96     }
 97 }
 98 /*用于输出链表的所有元素*/
 99 void PrintList()
100 {
101     node *p;
102     p=head;
103     int j=0;
104     while(p){
105         cout<<p->data<<endl;
106         j++;
107         p=p->next;
108     }
109     cout<<"This List have "<<j<<" numbers";
110 }
111 /*返回单链表中满足关系的第一个数的位置*/
112 int LocateElem(int e,char c)
113 {
114    if(c=='='){
115     if(ListEmpty()){
116         exit(-1);
117     }
118     int i=1;
119     node *p;
120     p=head;
121     while(p){
122         if(p->data==e){
123             return i;
124         }
125         i++;
126         p=p->next;
127      }
128    }
129 }
130 int main()
131 {
132     node *a;
133     for(int i=1; i<19; i++){
134     a=new node;
135     a->data=i;
136     AddList(a);
137     }
138     cout<<GetElem(18);
139 /*TO found error*/
140 //    node *p;
141 //    p=head;
142 //    while(p){
143 //        cout<<p->data<<endl;
144 //        p=p->next;
145 //    }
146 /*  eng */
147 
148 }

  在创建单链表时,创建一个新的结点可以使用 new 或 malloc 函数  ,但不能够在非主函数使用 new 和 malloc 函数。因为其并不能产生一个新的地址,一直会使用同一个存储空间创建结点,连接时便只有覆盖,而没有真正创建,所以在以后创建单链表的时候要注意。

转载于:https://www.cnblogs.com/wan123/p/11265165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值