线性表的链式存储及基本操作

线性表的链式存储

1、结构体
typedef struct LNode{
    ElemType data;  //节点的数据域
    struct LNode *next;  //节点的指针域,指针的类型为LNode,即指向节点
}LNode, *LinkList;   //LNode为结构体的名称
2、初始化
bool InitList(LinkList &L)
{
    L = (LNode *) malloc(sizeof(LNode));  //申请空间
    if(L==NULL)   //申请失败,返回false
        return false;
    L->next = NULL; //申请成功,将头节点置空
    return true;
}
3、头插法生成链表
LinkList List_HeadInsert(LinkList &L)
{
    LNode *s;int x;
    L =(LinkList)malloc(sizeof(LNode));
    L->next = NULL;
    cin >> x;
    while(x != 9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        s->next = L->next;
        L->next = s;
        cin >> x;
    }
    return L;
}
4、尾插法生成链表
LinkList List_TailInsert(LinkList &L)
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));
    LNode *s,*r=L;      //r为表尾指针
    cin >> x;
    while(x !=9999)
    {
        s = (LNode *)malloc(sizeof(LNode));
        s->data = x;
        s->next = NULL;
        r->next = s;
        r = s;
        cin >> x;
    }
}
5、按序号查找元素
LNode *GetElem(LinkList L, int i)
{
    int j = 1;
    LNode *p = L->next;
    if(i==0)
        return L;
    if(i<1)
        return NULL;
    while(p && j<i)
    {
        p = p->next;
        j++;
    }
    return p;
}
6、按值查找元素
LNode *LocateElem(LinkList L,ElemType e)
{
    LNode *p = L->next;
    while(p!=NULL && p->data!=e)
        p = p->next;
    return p;
}
7、插入节点
void InsertNode(LinkList &L, int location ,LNode *node)
{
    int j=1;
    //由于此处链表结构中没有长度属性,故在此处不进行长度越界判断
    LNode *p;
    p = GetElem(L,location-1);
    node->next = p->next;
    p->next = node;
}
8、删除节点
void DeleteNode(LinkList &L, LNode *p)
{
    LNode *q;
    q=p->next;
    p->next = q->next;
    free(q);
}

以上是所有的函数实现的功能,下面是完整程序。

9、完整代码
#include<iostream>
#include<malloc.h>
#define ElemType int

using namespace std;
typedef struct LNode{
    ElemType data;
    struct LNode *next;
}LNode, *LinkList;

bool InitList(LinkList &L)
{
    L = (LNode *) malloc(sizeof(LNode));
    if(L==NULL)
        return false;
    L->next = NULL;
    return true;
}

LinkList List_HeadInsert(LinkList &L)
{
    LNode *s;int x;
    L =(LinkList)malloc(sizeof(LNode));
    L->next = NULL;
    cin >> x;
    while(x != 9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        s->next = L->next;
        L->next = s;
        cin >> x;
    }
    return L;
}

LinkList List_TailInsert(LinkList &L)
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));
    LNode *s,*r=L;      //r为表尾指针
    cin >> x;
    while(x !=9999)
    {
        s = (LNode *)malloc(sizeof(LNode));
        s->data = x;
        s->next = NULL;
        r->next = s;
        r = s;
        cin >> x;
    }
}

//按照序号查找元素
LNode *GetElem(LinkList L, int i)
{
    int j = 1;
    LNode *p = L->next;
    if(i==0)
        return L;
    if(i<1)
        return NULL;
    while(p && j<i)
    {
        p = p->next;
        j++;
    }
    return p;
}

//按值查找元素
LNode *LocateElem(LinkList L,ElemType e)
{
    LNode *p = L->next;
    while(p!=NULL && p->data!=e)
        p = p->next;
    return p;
}
void printLinkList(LinkList L)
{
    L = L->next;
    while(L != NULL)
    {
        cout << L->data << " ";
        L = L->next;
    }
    cout << endl;
}

//插入节点
void InsertNode(LinkList &L, int location ,LNode *node)
{
    int j=1;
    //由于此处链表结构中没有长度属性,故在此处不进行长度越界判断
    LNode *p;
    p = GetElem(L,location-1);
    node->next = p->next;
    p->next = node;
}

void DeleteNode(LinkList &L, LNode *p)
{
    LNode *q;
    q=p->next;
    p->next = q->next;
    free(q);
}

int main()
{
    LinkList L1;
    InitList(L1);
    List_TailInsert(L1);
    printLinkList(L1);

    LNode *node;
    node = LocateElem(L1,3);
    cout << node->data << endl;

    LNode *n;
    n->data = 0;
    n->next = NULL;
    InsertNode(L1,2,n);
    printLinkList(L1);
    return 0;
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值