线性表的链式表示和实现

下面是线性表的链式表示和实现(c++语言,非伪代码)
形态:

实现:
/**************************************
线性表的链式表示和实现
    
    带头结点的单链表,头结点指针域指向首节点,数据域为链表长度

by Rowandjj
date 2014/3.27
***************************************/
#include<iostream>
using namespace std;

#define OK 1//操作成功
#define ERROR -1//操作失败
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1//内存分配失败

typedef int Status;//返回状态
typedef int ElemType;//元素类型 
typedef int Bool;
/*线性表的单链表存储结构*/
typedef struct _NODE_
{
    ElemType elem;
    struct _NODE_ *next;
}Node,*pNode,*LinkedList;

/* 操作定义 */
Status InitList_L(LinkedList&);//构造一个空的线性表
Status DestroyList_L(LinkedList&);//销毁线性表
Bool ListEmpty_L(LinkedList);//判断是否为空表
int ListLength_L(LinkedList);//返回线性表长度
void GetElem_L(LinkedList,int,ElemType&);//获取并返回线性表中指定位置的元素(1<=index<=ListLength)
void PriorElem_L(LinkedList,ElemType,ElemType&);//返回指定元素的前驱元素,若是第一个元素,则前驱未定义
void NextElem_L(LinkedList,ElemType,ElemType&);//返回指定元素的后继,若是最后一个元素,则后继未定义
Status ListInsert_L(LinkedList&,int,ElemType);//在指定位置上插入元素
Status ListDelete_L(LinkedList&,int,ElemType&);//删除指定位置上的元素
void ListTraverse_L(LinkedList);//遍历顺序表
void CreateList_L(LinkedList&,int);//创建链表,n为元素个数

/*具体实现*/
Status InitList_L(LinkedList& L)
{
    //初始化单链表,仅创建一个头结点,头结点数据域存放链表长度
    L = (pNode)malloc(sizeof(Node));
    if(!L)
    {
        exit(OVERFLOW);
    }
    L->elem = 0;
    L->next = NULL;
    return OK;
}
Bool ListEmpty_L(LinkedList L)
{
    if(!L)
    {
        exit(ERROR);
    }
    return L->elem == 0;
}
Status ListInsert_L(LinkedList &L,int i,ElemType e)// 1<=i<=len
{
    if(!L || i<=0)
    {
        exit(ERROR);
    }
    pNode p = L;
    int j = 0;
    while(p && j < i-1)
    {
        p = p->next;
        ++j;
    }
    if(!p || j > i-1)
    {
        return ERROR;
    }
    pNode pNew = (pNode)malloc(sizeof(Node));
    if(!pNew)
    {
        exit(OVERFLOW);
    }
    pNew->next = p->next;
    p->next = pNew;
    pNew->elem = e;
    L->elem++;
    return OK;
}
Status ListDelete_L(LinkedList &L,int i,ElemType &e)
{
    if(!L || i <= 0)
    {
        exit(ERROR);
    }
    pNode p = L;
    int index = 0;
    while(p->next && index<i-1)
    {
        p = p->next;
        index++;
    }
    pNode q = p->next;
    p->next = p->next->next;
    e = q->elem;
    free(q);
    L->elem--;
    return OK;
}
void CreateList_L(LinkedList &L,int n)
{
    if(!L)
    {
        exit(ERROR);
    }
    ElemType e;
    for(int i = 0; i < n; i++)
    {
        
        cin>>e;
        pNode pNew = (pNode)malloc(sizeof(Node));
        if(!pNew)
        {
            exit(OVERFLOW);
        }
        pNew->next = L->next;
        L->next = pNew;
        
        pNew->elem = e;
        L->elem++;
    }
    
}
void ListTraverse_L(LinkedList L)
{
    if(!L)
    {
        exit(ERROR);
    }
    pNode p = L->next;
    while(p)
    {
        cout<<p->elem<<" ";
        p = p->next;
    }
    cout<<endl;
}
int ListLength_L(LinkedList L)
{
    return L->elem;
}
void GetElem_L(LinkedList L,int i,ElemType &e)
{
    if(!L)
    {
        exit(ERROR);
    }
    int j = 1;
    pNode p = L->next;
    while(p && j < i)
    {
        p = p->next;
        j++;
    }
    if(j > i || !p)
    {
        return;
    }
    e = p->elem;
}
void PriorElem_L(LinkedList L,ElemType e,ElemType& i)
{
    if(!L)
    {
        exit(ERROR);
    }
    pNode p = L;
    int j = 0;
    while(p->next && p->next->elem != e)
    {
        p = p->next;
    }
    if(!p->next)
    {
        return;
    }
    i = p->elem;
}
void NextElem_L(LinkedList L,ElemType e,ElemType& i)
{
    if(!L)
    {
        exit(ERROR);
    }
    pNode p = L;
    while(p->next && p->elem != e)
    {
        p = p->next;    
    }
    if(!p->next)
    {
        return;
    }
    i = p->next->elem;
}
Status DestroyList_L(LinkedList &L)
{
    if(!L)
    {
        exit(ERROR);
    }
    
    ElemType e;
    int size = L->elem;
    for(int i = 1; i <= size; i++)
    {    
        ListDelete_L(L,1,e);
        cout<<"destroy:"<<e<<endl;
    }
    free(L);
    return OK;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值