数据结构----2、链表代码可直接运行 C语言

#include<bits/stdc++.h>
using namespace std;
struct lists{
 int data;
 struct lists *node;
};
int output(lists * head)
{
 lists * p;
 p=head->node;
 while(p!=NULL)
 {
  cout<<p->data;
  p=p->node;
  } 
}
int creat(lists * head )
{
int n;
cout<<"输入创建个数:";
cin>>n;
cout<<"输入创建的每个数:"; 
lists *m=head;
while(n--)
{
 lists * p;
 p=(lists *)malloc(sizeof(lists));
 cin>>p->data;p->node=NULL;
 m->node=p;
 m=p;
 }
 cout<<"创建完成"<<endl; 
 return 0;
 } 
 int insert(lists * head)
{ lists  *q;
q=(lists*)malloc(sizeof(lists));
int n,Idate;
cout<<"输入插入位置:";
cin>>n;cout<<endl;
cout<<"输入插入数据:";
cin>>Idate;
q->data=Idate;
lists * p;
int i=0;
 for( p=head;i<n-1&&p->node!=NULL;i++)
{   
    p=p->node;
 } 
  q->node=p->node;
  p->node=q;
  cout<<"插入完成"<<endl; 
return 0;
    
 }
 int find(lists *head)
{
 int n;
 cout<<"输入查找的数:";
 cin>>n;
 lists *q;
 q=head;
while(q!=NULL)
{
 q=q->node;
 if(q->data==n)
 {cout<<"找到了"<<endl;
 return 0; 
 }
}
cout<<"该链表中没有"<<endl; 
}
int delet ( lists * head )
{
 int i;
 cout<<"输入删除第几个数:";
 cin>>i; 
    lists * q, * p;
   int n;
    for ( n=0, q = head;  n<i-1 && q->node!=NULL; ++n )
       q = q->node;               
    if  ( i>0 && q->node != NULL )   
    { 
        p = q->node;             
        q->node = p->node; 
       free ( p );  
    cout<<"删除完成"<<endl; 
    }
}
void hb(lists *a,lists *b,lists *c)
{
 lists *pa,*pb,*pc;
 pa=a->node;
 pb=b->node;
 pc=c=a;
 while(pa&&pb)
 {
  if(pa->data<=pb->data)
  {
   pc->node=pa;
   pc=pa;
   pa=pa->node;
  }
  else
  {
   pc->node=pb;
   pc=pb;
   pb=pb->node;
  }
  
 }
    pc->node=pa?pa:pb;
 free(b); 
 
 } 
 void ym()
{
 cout<<"1、初始化"<<endl<<"2、插入"<<endl<<"3、删除"<<endl<<"4、查找"<<endl<<"5、全表浏览"<<endl<<"6、合并链表"<<endl<<"7、退出程序"<<endl; 
 
}
int main()
{
 lists * head,*a;
 lists *p;
    p=(lists*)malloc(sizeof(lists));
    p->node=NULL;
    head = p;
    a=head;
   ym();
   while(1)
   {int n;
    cin>>n;
   switch(n){
    
    case 1:
    creat(head) ;
    continue;
 case 2:
  insert(head);
  continue;
 case 3:
  delet(head);
  continue;
 case 4:
  find(head);
  continue;
 case 5:
  output(head);
  continue;
   case 6:
  printf("再创建一个链表");
  lists * head1,*a1;
     lists *p1;
        p1=(lists*)malloc(sizeof(lists));
        p1->node=NULL;
        head1 = p1;
        lists * head2,*a2;
     lists *p2;
        p2=(lists*)malloc(sizeof(lists));
        p2->node=NULL;
        head2 = p2;
        creat(head1);
  hb(head,head1,head2); 
  
  continue;
 case 7:
  exit(0);
    
   }
   }
   return 0;}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值