数据结构之链式线性表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37667021/article/details/78034987

带头结点的


/*
本链表含有头结点,结点从1开始。

2017.9.27 by 横济沧海
*/
#include<stdio.h>
#include<string.h>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int status;
typedef struct LNODE
{
    int data;
    struct LNODE *next;
} LNode,*Linklist;
status initLinset(Linklist &head)//初始化
{
    head=(Linklist)malloc(sizeof(LNODE));
    if(!head)return ERROR;
    head->next=NULL;
    return OK;
}
status clearList(Linklist &head)//将整表进行清空
{
    Linklist p,q;
    p=head->next;
    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }
    head->next=NULL;
    return OK;
}
status DestroyList(Linklist &head)//将整表删除掉
{
    Linklist p,q;
    p=head->next;
    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }
    free(head);
    head=NULL;//free指针记得赋为NULL

    return OK;
}
status insertList(Linklist &L,int i,int e)//在i前插入e
{
    int j=0,k;
    Linklist p,q;
    p=L;
    if(j>i-1)return ERROR;
    while(p&&j<i-1)
    {    
    //这里要好好理解,i=0时,p指向头结点,当i=1时,p指向首结点
    //
    //
        j++;
        p=p->next;
    }
    
    if(p!=NULL)//如果p为空那么就没有找到i-1这个节点,就说明i的值不合法
    {
        q=(Linklist)malloc(sizeof(LNODE));
        if(q!=NULL)
        {
            q->data=e;
            q->next=p->next;
            p->next=q;
            return OK;
        }
        else return ERROR;

    }
    else return ERROR;
}

status deleteList(Linklist &L,int i)//删除I节点
{
    int j=0,k;
    Linklist p,q;
    p=L;
    if(j>i-1)return ERROR;
    while(p&&j<i-1)
    {
        j++;
        p=p->next;
    }
    if(p!=NULL)
    {
        q=p->next;
        p->next=q->next;
        free(q);
        return OK;
    }
    else return ERROR;
}
status updataList(Linklist &L,int i,int e)//将i处的值更新为e
{
    int j=0,k;
    Linklist p,q;
    p=L;
    if(j>i-1)return ERROR;
    while(p&&j<i)
    {
        j++;
        p=p->next;
    }
    if(p!=NULL)
    {
        p->data=e;
        return OK;
    }
    else return ERROR;
}

int getElem(Linklist L,int i)//得到i处的值
{
    int j=0,k;
    Linklist p,q;
    p=L;
    if(j>i-1)return ERROR;
    while(p&&j<i)
    {
        j++;
        p=p->next;
    }
    if(!p)return -1;
    else return p->data;
}

int query(Linklist L,int e)//查询是否有e这个元素
{
    int j=0,k;
    Linklist p,q;
    p=L->next;
    while(p)
    {
        j++;
        if(p->data==e)return j;
        else p=p->next;
    }
    return -1;
}

void traverse(Linklist L)//链表的遍历
{
    Linklist p,q;
    p=L->next;
    while(p)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
int getlength(Linklist L)//q取得长度
{
    Linklist p,q;
    int j=0;
    p=L->next;
    while(p)
    {
        j++;
        p=p->next;
    }
    return j;
}

int main()
{
    int i,j,k;
    Linklist head;
    initLinset(head);
    for(i=1; i<=10; i++)
        insertList(head,i,i);
    traverse(head);

    insertList(head,2,100);
    traverse(head);
    insertList(head,1,99);
    traverse(head);
    insertList(head,5,98);
    traverse(head);

    deleteList(head,1);
    traverse(head);
    deleteList(head,9);
    traverse(head);
    updataList(head,4,55);
    updataList(head,1,22);
    traverse(head);
    printf("%d %d %d\n",getElem(head,3),getlength(head),query(head,33));

    clearList(head);
    printf("%d\n",getlength(head));
    DestroyList(head);
    if(head==NULL)printf("This List has been destroyed!");

    return 0;
}




猜你喜欢

转载自blog.csdn.net/m0_37667021/article/details/78034987
今日推荐