首页 > 其他 > 详细

单链表

时间:2015-11-28 13:35:15      阅读:266      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <cstdlib>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int Status;
typedef int ElemType;

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

void InitList(LinkList &L)
{
    L = (LinkList)malloc(sizeof(LNode));
    if(!L)
        exit(OVERFLOW);
    L->next = NULL;
}

void DestroyList(LinkList &L)
{
    LinkList q;
    while(L)
    {
        q = L->next;
        free(L);
        L = q;
    }
}

void ClearList(LinkList L)
{
    LinkList p,q;
    p = L->next;
    while(p)
    {
        q = p->next;
        free(p);
        p = q;
    }
    L->next = NULL;
}

bool ListEmpty(LinkList L)
{
    if(L->next)
        return false;
    else
        return true;
}

int ListLength(LinkList L)
{
    int i = 0;
    LinkList p = L->next;
    while(p)
    {
        i++;
        p = p->next;
    }
    return i;
}

Status GetElem(LinkList L,int i,ElemType &e)
{
    int j = 1;
    LinkList p = L->next;
    while(p && j < i)
    {
        p = p->next;
        j++;
    }
    if(!p || j > i)
        return ERROR;
    e = p->data;
    return OK;
}

int LocateElem(LinkList L,ElemType e)
{
    int i = 0;
    LinkList p = L->next;
    while(p)
    {
        i++;
        if(p->data == e)
            return i;
        p = p->next;
    }
    return 0;
}

Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
{
    LinkList q, p = L->next;
    while(p->next)
    {
        q = p->next;
        if(q->data == cur_e)
        {
            pre_e = p->data;
            return OK;
        }
        p = q;
    }
    return INFEASIBLE;
}

Status NextElem(LinkList L,ElemType cur_e,ElemType &next_e)
{
    LinkList p = L->next;
    while(p->next)
    {
        if(p->data == cur_e)
        {
            next_e = p->next->data;
            return OK;
        }
        p = p->next;
    }
    return INFEASIBLE;
}

Status ListInsert(LinkList L,int i,ElemType e)
{
    int j = 0;
    LinkList p = L,s;
    while(p && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if(!p || j > i - 1)
        return ERROR;
    s = (LinkList)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return OK;
}

Status ListDelete(LinkList L,int i,ElemType &e)
{
    int j = 0;
    LinkList p = L,q;
    while(p->next && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if(!p->next || j > i - 1)
        return ERROR;
    q = p->next;
    p->next = q->next;
    e = q->data;
    free(q);
    return OK;
}

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

int main()
{
    LinkList L;
    InitList(L);
    cout<<ListLength(L)<<endl;
    for(int i = 1; i <= 10; i++)
        ListInsert(L,i,i);
    ListTraverse(L);
    return 0;
}

 

单链表

原文:http://www.cnblogs.com/chenyang920/p/5002495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!