线性表(链表)——单链表、双链表、循环单链表、循环双链表

单链表的定义及基本操作

#include<iostream>
using namespace std;
typedef struct Node//定义一个结点类的结构体类型
{
    int data;//数据域
    struct Node *next;//指针域
}node,* pnode;
//node * 和pnode 等价,在使用中node *强调这是一个结点,pnode 强调这是一个链表

//基本操作——按序号查找节点值
node * GetElem(pnode L,int pos)//函数含义:获取链表中第pos个结点
{
    if(pos==0)
        return L;
    if(pos<1)
        return NULL;
    int j=1;
    //定义一个结点用来存放顺序表中的第一个结点
    node * fnode=L->next;
    while(fnode!=NULL&&j<pos)
    {
        fnode=fnode->next;
        j++;
    }
    return fnode;
}

//基本操作——按值查找表结点
node *LocateElem(pnode L,int elem)
{
    node * x=L->next;//x指向链表当中的第一个结点
    while(x!=NULL)
    {
        if(x->data==elem)
            break;
        x=x->next;
    }
    return x;
}
//基本操作——利用尾插法建立单链表
void LinkList(pnode &L,int length)
{
    node *enode,*tnode;
    tnode=L;
    int x;
    for(int i=0;i<length;i++)
    {
        enode=(node *)malloc(sizeof(node));
        cin>>x;
        enode->data=x;
        tnode->next=enode;
        tnode=enode;
    }
    cout<<"输入完毕,开始打印表中元素"<<endl;
    tnode->next=NULL;
}
//基本操作——判断是否为空表
bool IsEmpty(pnode L)
{
    return L->next==NULL;
}
//打印单链表
void PrintList(pnode L)
{
    if(IsEmpty(L))
        cout<<"NULL"<<endl;
    else
    {
        //f指向链表中的第一个结点
        node * f=L->next;
        while(f!=NULL)
        {
            cout<<f->data<<" ";
            f=f->next;
        }
        cout<<endl;
    }
}
//基本操作——求单链表长度
int GetLength(pnode L)
{
    int length=0;
    node * x=L->next;
    while(x!=NULL)
    {
        length++;
        x=x->next;
    }
    return length;
}
//基本操作——插入结点
pnode InsertList(pnode &L,int pos,int elem)
{
    //找到插入位置的前驱节点
    node *prenode=GetElem(L,pos-1);
    node *innode=(node *)malloc(sizeof(node));
    innode->data=elem;
    innode->next=prenode->next;
    prenode->next=innode;
    return L;
}
//基本操作——删除结点(删除单链表中的第i个结点)
pnode DeleteList(pnode &L,int pos)
{
    node * p=GetElem(L,pos-1);//获取删除结点的前驱节点
    node * q=p->next;//q为待删除的结点
    p->next=q->next;//将q从链中断开
    free(q);
    return L;
}
int main()
{
    int n;
    cout<<"输入单链表长度:"<<endl;
    cin>>n;
    pnode L;
    L=(node *)malloc(sizeof(node));
    L->next=NULL;//初始化一个空表
    LinkList(L,n);
    cout<<"表长为:"<<GetLength(L)<<endl;
    PrintList(L);
    //创建以一个结点并分配存储空间
    node *getnode=(node *)malloc(sizeof(node));
    int pos;
    cin>>pos;
    cout<<"获取第"<<pos<<"个元素的值:"<<endl;
    getnode=GetElem(L,pos);
    cout<<getnode->data<<endl;
    int elem;
    cout<<"输入你想查找元素的值:";
    cin>>elem;
    cout<<"获取值为"<<elem<<"的位置:"<<endl;
    node *loc=LocateElem(L,elem);
    if(loc==NULL)
        cout<<"表中无此元素"<<endl;
    else
        cout<<loc->data<<endl;
    cout<<"在表中某个位置插入一个元素,插入位置以及插入元素分别为:"<<endl;
    int a,b;
    cin>>a>>b;
    InsertList(L,a,b);
    cout<<"现在单链表的元素和长分别是:"<<endl;
    PrintList(L);
    cout<<GetLength(L)<<endl;
    DeleteList(L,11);
    PrintList(L);
    DeleteList(L,1);
    PrintList(L);
    return 0;
}

循环单链表:

#include<iostream>
using namespace std;
typedef struct Node
{
    int data;
    struct Node *next;
}node,* LinkList;
//初始化单链表
bool InitLinkList(LinkList &L)
{
    L=(node *)malloc(sizeof(node));
    if(L==NULL)
        return false;
    L->next=L;
    return true;
}

//判断循环单链表是否为空
bool isEmpty(LinkList L)
{
    if(L->next==NULL)
        return true;
    else
        return false;
}

//判断一个结点是否是尾结点
bool isTail(LinkList L,node *p)
{
    if(p->next==L)
        return true;
    else
        return false;
}

循环双链表:

#include<iostream>
using namespace std;
typedef struct Node
{
    int data;
    struct Node *next;
    struct Node *prior;
}node,*DLinkList;
bool InitDLinkList(DLinkList &L)
{
    L=(node *)malloc(sizeof(node));
    if(L==NULL)
        return false;
    L->next=L;
    L->prior=L;
    return true;
}
bool isEmpty(DLinkList L)
{
    if(L->next==L)
        return true;
    else
        return false;
}
bool isTail(DLinkList L,node *p)
{
    if(p->next==L)
        return true;
    else
        return false;
}
//将s插入p后边
bool InsertList(node *s,node *p)
{
    //不用考虑边界情况
    s->next=p->next;
    p->next->prior=s;//
    p->next=s;
    s->prior=p;
}
bool DeleteList(node *p)//删除p结点的后继节点q
{
    node *q=p->next;
    p->next=q->next;
    q->next->prior=p;
    free(q);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值