对单链表的相关操作

单链表的学习是数据结构中最基本的知识,这个例子是我现实的一个单链表,并对这个单链表执行一系列的操作。

下图是实现的截图:


下面给出实现的C++代码:

#include <cstdlib>
#include <iostream>

using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;        
}LNode;
int Get_i(LNode *L)
{
cout<<"输入查找的是第几个元素"; 
int a,i=1;
LNode *k=L;
cin>>a;
while(i<=a&&k->next!=NULL)
{
i++;
k=k->next;                        
}
cout<<"第"<<a<<"个元素是"<<k->data<<endl;    
}
int Get_value(LNode *L)
{
cout<<"输入查找元素的值是:";
int count=1,x;
LNode *k=L;
cin>>x;
while(k->next->data!=x&&k->next!=NULL)
{
count++;
k=k->next;                                      
}
cout<<"所查找的元素"<<x<<"是第"<<count<<"个"<<endl;    
}
void Insert(LNode *L)
{
cout<<"输入需要插入数据的位置:";
int i,j=1,k;  //i是插入的位置 ,k是插入的数据 
LNode *p,*q,*r;
r=L;
p=L;
cin>>i;
cout<<"  "<<"需要插入的数据是:";
cin>>k; 
while(j<i)
{
p=p->next;
j++;
}
q=(LNode*)malloc(sizeof(LNode));
q->data=k;
q->next=p->next;
p->next=q;
cout<<"现在的链表中数据是"<<endl;
while(r->next!=NULL)
{
cout<<r->next->data<<"  ";
r=r->next;                    
}     
}
void Delete_i(LNode *L)
{
cout<<"输入需要删除数据的位置:";
int i,j=0;
cin>>i;
LNode *p=L,*q,*r;
r=L;
while(j<i)
{
j++;
q=p;
p=p->next;         
}
q->next=p->next;
free(p);
cout<<"现在的链表中数据是"<<endl;
while(r->next!=NULL)
{
cout<<r->next->data<<"  ";
r=r->next;                    
} 
cout<<endl;    
}
void Delete_value(LNode *L)
{
cout<<"输入需要删除数据的值:";
int x;
cin>>x;
LNode *p=L,*q,*r=L;
while(p->next->data!=x)
{
q=p;
p=p->next;                       
}
q=p;
p=p->next; 
q->next=p->next;
free(p);
cout<<"现在的链表中数据是"<<endl;
while(r->next!=NULL)
{
cout<<r->next->data<<"  ";
r=r->next;                    
} 
cout<<endl;        
}
void Join(LNode *L)
{
    int x;
    LNode *head,*p,*q,*r;
    head=(LNode*)malloc(sizeof(LNode));
    head->next=NULL;
    q=head;
    cout<<"请输入数据建立另一个链表并同样以输入数字0为结束标志:"<<endl; 
    while(1)
    {
    cin>>x;
    if(x==0)
    break;
    else
    {
    p=(LNode*)malloc(sizeof(LNode));
    p->data=x;
    p->next=q->next;  //采用尾插法,正序输出链表中数据 
    q->next=p;
    q=p;    
    }        
    }
    cout<<"第二个链表存数为:"<<endl;
    r=head;
    while(r->next!=NULL)
    {
    cout<<r->next->data<<"  ";
    r=r->next;                    
    }
    cout<<endl;
    LNode *a,*b;
    a=L;
    b=L;
    while(a->next!=NULL)
    {
    a=a->next;                    
    }
    a->next=head->next;
    free(head);
    cout<<"现在的链表中数据是"<<endl;
    while(b->next!=NULL)
    {
    cout<<b->next->data<<"  ";
    b=b->next;                    
    } 
cout<<endl;      
} 
int main(int argc, char *argv[])
{
    int x;
    LNode *head,*p,*q,*r;
    head=(LNode*)malloc(sizeof(LNode));
    head->next=NULL;
    q=head;
    cout<<"请输入数据建链表并以输入数字0为结束标志:"<<endl; 
    while(1)
    {
    cin>>x;
    if(x==0)
    break;
    else
    {
    p=(LNode*)malloc(sizeof(LNode));
    p->data=x;
    p->next=q->next;  //采用尾插法,正序输出链表中数据 
    q->next=p;
    q=p;    
    }        
    }
    cout<<"链表存数为:"<<endl;
    r=head;
    while(r->next!=NULL)
    {
    cout<<r->next->data<<"  ";
    r=r->next;                    
    }
    cout<<endl;
    cout<<"************************************************"<<endl;
    cout<<"0:退出系统"<<endl; 
    cout<<"1:按位置查找元素"<<endl;
    //Get_i(head);
    cout<<"2:按值查找元素"<<endl;
    //Get_value(head);
    cout<<"3: 执行插入操作"<<endl; 
    //Insert(head); 
    cout<<"4: 按位置删除元素"<<endl;
    //Delete_i(head);
    cout<<"5: 按值删除数据"<<endl;
    //Delete_value(head);
    cout<<"6: 建立一个新的链表并与前一个链表连接"; 
    //Join(head);
    cout<<endl<<"************************************************"<<endl;
    int t;
   do{
    cout<<"请输入选项执行相应的操作:";
    cin>>t;
    switch(t)
              {
      case 0:    cout<<"***********退出系统************"<<endl; break; 
      case 1:    Get_i(head); break;              
      case 2:    Get_value(head);  break;            
      case 3:    Insert(head);   break;              
      case 4:    Delete_i(head);  break;              
      case 5:    Delete_value(head);  break;              
      case 6:    Join(head);    break;
           default :  cout<<"ERROR"<<endl;  break;             
              }
              }while(t!=0);
    system("PAUSE");
    return EXIT_SUCCESS;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值