链式线性表

#include <iostream>
#include<string>
using namespace std;
int count=0;
struct node
{
  int    data;
  node * next; 
};
node *Creat()
{
  node *p1,*p2,*head;
  int a;
  head=0;
  cout<<"输入数据以-1结尾"<<endl;
  cin>>a;
  while(a!=-1)
  {
    p1=new node;
    p1->data=a;
    count++;
    if(head==0)
    {
      head=p1;
      p2=p1;
    }
    else
    {
      p2->next=p1;
      p2=p1;
    }
    cin>>a;
  }
  p2->next=0;
  return(head);
}
node *Delete (node * head,int num)
{
  node *p1,*p2;
  if(head->data==num)
  {
    p1=head;
    head=head->next;
    delete p1;
  }
  else
  {
    p1=head;
    while(p1!=NULL)
    {
      if(p1->next->data==num)
      {
        p2=p1->next;
        p1->next=p2->next;
        delete p2;
      }
      p1=p1->next;
    }
  }
  return (head);
}
node *Add(node * head,int num,int con)
{
  node *p1,*p2;
  if(num-1==0)
  {
    p1->next=head;
    head=p1;
    head->data=con;
  }
  else if(num-1>0)
  {
    p1=head;
    int ii=1;
    p2=new node;
    while(p1!=NULL)
   
      ii++;
      if(ii==num)
      {
        p2->next=p1->next;
        p2->data=con;
        p1->next=p2;
      }
      p1=p1->next;
    }
  }
  return head;
}
void view(node *head)
{
  cout <<"此时链表内容为"<<endl;
  node *p1;
  for (p1=head;p1!=NULL;p1=p1->next)
  {
    cout<<p1->data<<endl;
  }
}
node *Change(node * head,int num,int con)
{
  node *p1,*p2;
  if(num-1==0)
  {
    head->data=con;
  }
  else
  {
    p1=head;
    int o=1;
    while(p1!=NULL)
    {
      if (o==num)
      {
        p1->data=con;
      }
      o++;
      p1=p1->next;
    }
  }
  return head;
}
int Search(node *head,int num)
{
  int p=1;
  int save;
  node *p1=head;
  while(p1!=NULL)
  {
    if(p==num)
    {
      save=p1->data;
    }
    p1=p1->next;
    p++;
  }
  return save;
}
int main()
{
  node *head;
  head=Creat();
  string a="continue";
  while(a=="continue")
  {
    cout<<"请输入要执行的操作"<<endl;
    cout<<"1:增加        "<<"2:删除        "<<"3:修改        "<<"4:查找        "<<endl;
    int count;
    cin>>count;
    switch(count)
    {
    case 1:
      cout<<"要增加的位置"<<endl;
      int position;
      cin>>position;
      cout<<"要增加的数"<<endl;
      int w;
      cin>>w;
      head=Add(head,position,w);
      view(head);
      break;
    case 2:
      cout<<"要删除的数"<<endl;
      int dp;
      cin>>dp;
      head=Delete(head,dp);
      view(head);
      break;
    case 3:
      cout<<"要修改元素的位置"<<endl;
      int cp;
      cin>>cp;
      cout<<"要修改成的值"<<endl;
      int cp2;
      cin>>cp2;
      head=Change(head,cp,cp2);
      view(head);
      break;
    case 4:
      cout<<"要查找的元素的位置"<<endl;
      int sp;
      cin>>sp;
      cout<<"该位置的元素为"<<Search(head,sp)<<endl;
      break;
    }
    cout<<"请输入下一步操作"<<endl;
    cout<<"输入continue继续操作,输入其他任意键退出程序"<<endl;
    cin>>a;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值