数据结构——单链表基本操作

实现单链表的初始化,头插法建表,尾插法建表,查找元素,插入元素,删除元素等功能

#include <bits/stdc++.h>
using namespace std;

#define ElemType char
typedef struct Node
{
    ElemType data;
    struct Node *next;
}Node,*Linklist;

//初始化单链表
void InitList(Linklist *L)
{
    *L=(Node *)malloc(sizeof(Node));//建立头结点
    (*L)->next=NULL;//建立空的单链表L
}
//头插法建表
void CreateFromHead(Linklist L)
{
    Node *s;
    ElemType c;
    int flag =1;
    while(flag)
    {
        c=getchar();
        if(c!='$')
        {
            s=(Node *)malloc(sizeof(Node));
            s->data=c;
            s->next=L->next;
            L->next=s;
        }
        else
            flag=0;
    }
}
 //尾插法建表
 void CreateFromTail(Linklist L)
 {
     Node *r,*s;
     int flag=1;
     r=L;
     ElemType c;
     while(flag)
     {
         c=getchar();
         if(c!='$')
         {
             s=(Node *)malloc(sizeof(Node));
             s->data=c;
             r->next=s;
             r=s;
         }
         else{
            flag=0;
            r->next=NULL;
         }
     }
 }
 //按序号查找
Node*  Get(Linklist L,int i)
 {
     int j;
     Node *p;
     if(i<=0)
     {
       cout<<"输入序号不合法"<<endl;
       return NULL;
     }
     p=L;
     j=0;
     while((p->next!=NULL)&&(j<i))
     {
         p=p->next;
         j++;
     }
     if(i==j)
        return p;
     else
     {
        cout<<"输入序号不合法"<<endl;
        return NULL;
     }

 }
 //按值查找,返回它在链表中的位置
int  Locate(Linklist L,ElemType key)
 {
    Node *p;
    int loc=1;
    p=L->next;
    while(p!=NULL)
    {
        if(p->data!=key)
        {
            p=p->next;
            loc++;
        }
        else
            break;
    }
    return loc;
 }
 //求链表长度
 int Len(Linklist L)
 {
     Node *p;
     p=L->next;
     int j=0;
     while(p!=NULL)
     {
         p=p->next;
         j++;
     }
     return j;
 }
 //插入操作
 void Insert(Linklist L,int i,ElemType e)
 {
     Node *pre,*s;
     int k;
     if(i<=0)
        return ;
     pre=L;
     k=0;
     while(pre!=NULL&&k<i-1)
     {
         pre=pre->next;
         k++;
     }
     if(pre==NULL)
     {
         cout<<"插入位置不合法"<<endl;
         return ;
     }
     s=(Node *)malloc(sizeof(Node));
     s->data=e;
     s->next=pre->next;
     pre->next=s;
     return ;
 }
 //删除第i个元素
 void Delete(Linklist L,int i,ElemType *e)
 {

     Node *pre,*r;
     int k=0;
     if(i<0)
     {
         cout<<"删除位置不合理!"<<endl;
         return ;
     }
     pre=L;
     while(pre->next!=NULL&&k<i-1)
     {
         pre=pre->next;
         k++;
     }
     if(pre->next==NULL)
     {
         cout<<"删除位置不合理!"<<endl;
         return ;
     }
     r=pre->next;
     pre->next=r->next;
     *e = r->data;
     free(r);
     return ;
 }
 void show(Linklist L)
 {
     Node *s=L->next;
     while(s!=NULL)
     {
         cout<<s->data<<" ";
         s=s->next;
     }
     cout<<endl;
 }
int main()
{
    Linklist L;
    InitList(&L);
    cout<<"请依次输入链表元素(以$结束):";
    CreateFromTail(L);
    //CreateFromTail(L);
    show(L);
    int len=Len(L);
    int op;
    cout<<"你的操作:";
    cin>>op;
    do
    {
        if(op==0)
        {
           cout<<"结束操作"<<endl;
           break;
        }
        if(op==1)
        {
          cout<<"按序号查找"<<endl;
          int e1;
          cout<<"输入待查找元素的位置:";
          cin>>e1;
          Node *get;
          get=Get(L,e1);
          if(get!=NULL)
              cout<<"查找成功!该元素的值为"<<get->data<<endl;
          cout<<endl;
        }
        else if(op==2)
        {
          cout<<"按值查找"<<endl;
          ElemType e2;
          cout<<"输入待查找的元素值:";
          cin>>e2;
          int location=Locate(L,e2);
          if(location>len)
             cout<<"查找失败!链表中不含该元素!"<<endl;
          else
              cout<<"查找成功!该元素的位置为"<<location<<endl;
          cout<<endl;
        }
        else if(op==3)
        {
           cout<<"链表长度为:"<<len<<endl;
           cout<<endl;
        }
        else if(op==4)
        {
           cout<<"插入"<<endl;
           ElemType e3;
           int loc1;
           cout<<"输入待插入的元素:";
           cin>>e3;
           cout<<"输入待插入的位置:";
           cin>>loc1;
           Insert(L,loc1,e3);
           cout<<"插入成功!更新线性表:";
           show(L);
           cout<<endl;
        }
        else if(op==5)
        {
            ElemType e4;
            cout<<"删除"<<endl;
            int loc2;
            cout<<"输入待删除元素的位置:";
            cin>>loc2;
            Delete(L,loc2,&e4);
            if(e4)
               cout<<"删除成功!更新线性表:";
            show(L);
            cout<<endl;
        }
        else if(op==6)
        {
            cout<<"查看当前链表:"<<endl;
            show(L);
            cout<<endl;
        }
        cout<<"你的操作:";
    }while(cin>>op);

    return 0;
}

效果展示:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值