线性表

线性表的顺序实现

找到再补

线性表的链式实现

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define ElemType int
#define Status int
#define ListSize 1000
using namespace std;

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}*LinkList;

Status Init_LinkList(LinkList &L)
{
    L = (LNode *)malloc(sizeof(LNode));
    L->next = NULL;
    return OK;
}

Status Insert_List(LinkList &L,int i,ElemType e)
{
    LinkList p,q;
    int j = 0;
    if(i==1)
    {
        p = (LNode*)malloc(sizeof(LNode));
        p->data = e;
        p->next = L->next;
        L->next = p;
    }
    else
    {
        p = L;
        while(p&&j<i-1)
        {
            p = p->next;
            ++j;
        }
        q = (LNode*)malloc(sizeof(LNode));
        q->next = NULL;
        q->data = e;
        p->next = q;
    }
    return OK;
}

Status List_Traverse(LinkList L)
{
    LinkList p;
    p = L->next;
    while(p)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;
}

Status List_Delete(LinkList &L,int pos)
{
    int cnt = 1;
    LinkList p,q;
    p = L->next;
    while(p&&(cnt<pos-1))
    {
        cnt++;
        p = p->next;
    }
    q = p->next;
    p->next = q->next;
    free(q);
}

Status List_Update(LinkList &L,int pos,ElemType e)
{
    int cnt = 1;
    LinkList p,q;
    p = L->next;
    while(p&&(cnt<pos))
    {
        cnt++;
        p = p->next;
    }
    p->data = e;
    return OK;
}

int main()
{
    LinkList L;
    Init_LinkList(L);
    ElemType e,n,pos;
    cout<<"输入数据的各数n:";
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>e;
        Insert_List(L,i,e);
    }
    cout<<"输入的数据如下:"<<endl;
    List_Traverse(L);
    cout<<"Input Elem e before x:"<<endl;
    cin>>e>>n;
    Insert_List(L,n,e);
    List_Traverse(L);
    cout<<"Input the del pos:";
    cin>>pos;
    List_Delete(L,pos);
    List_Traverse(L);
    cout<<"Input the change pos and Elem e:";
    cin>>pos>>e;
    List_Update(L,pos,e);
    List_Traverse(L);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值