Fork me on GitHub

线性表的顺序存储和链式存储实现

      这几天搞连通域的问题;其中用的数据结构就是顺序的数组实现的类似链表的操作,思想是一样的,但他没有写成标准的形式,总是感觉怪怪的。根据《中国大学MOOC-陈越、何钦铭-数据结构-2017春》学习计划,突然理解,线性表的顺序存储又分为静态的和动态的,即初始化的方法区别,在嵌入式的系统中用静态的,提前开辟一段内存较好。

/*!
 * \file 线性表的顺序存储实现.cpp
 *
 * \author ranjiewen
 * \date 三月 2017
 *
 * 
 */

#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef int Position;
typedef struct LNode *List;
#define  MAXSIVE 100

struct LNode
{
    ElemType Data[MAXSIVE];
    Position Last;  //数组的下标
};

/*初始化*/
List MakeEmpty()
{
    List L; 
    L = (List)malloc(sizeof(struct LNode));
    L->Last = -1; //初始元素位置为0
    return L;
}

#define ERROR -1
Position Find(List L, ElemType x)
{
    Position i = 0;
    while (i<=L->Last&&L->Data[i]!=x)
    {
        i++;
    }
    if (i>L->Last)
    {
        return ERROR;  //如果没有找到,返回错误信息
    }
    else
    {
        return i; //找到了返回存储位置
    }
}

/*插入*/
bool Insert(List L, ElemType X, Position P)
{ /* 在L的指定位置P前插入一个新元素X */
    Position i;

    if (L->Last == MAXSIVE - 1) {
        /* 表空间已满,不能插入 */
        printf("表满");
        return false;
    }
    if (P<0 || P>L->Last + 1) { /* 检查插入位置的合法性 */
        printf("位置不合法");
        return false;
    }
    for (i = L->Last; i >= P; i--)
        L->Data[i + 1] = L->Data[i]; /* 将位置P及以后的元素顺序向后移动 */
    L->Data[P] = X;  /* 新元素插入 */
    L->Last++;       /* Last仍指向最后元素 */
    return true;
}

/* 删除 */
bool Delete(List L, Position P)
{ /* 从L中删除指定位置P的元素 */
    Position i;

    if (P<0 || P>L->Last) { /* 检查空表及删除位置的合法性 */
        printf("位置%d不存在元素", P);
        return false;
    }
    for (i = P + 1; i <= L->Last; i++)
        L->Data[i - 1] = L->Data[i]; /* 将位置P+1及以后的元素顺序向前移动 */
    L->Last--; /* Last仍指向最后元素 */
    return true;
}

//表长操作顺序存储即数组大小

int main()
{
    //List L = MakeEmpty(); //正确的初始化

    //List L = NULL;//不合理的初始化

    struct LNode list_; //合理的初始化
    list_.Last = -1; //必须赋值;
    for (int i = 0; i<4;i++)
    {
        list_.Data[i] = 10 + i;
        list_.Last++;
    }
    List L = &list_; 

    for (int i = 9; i > 0; i--)
    {
        Insert(L, i, 9 - i);
    }

    for (int i = 0; i <= L->Last;i++)
    {
        printf("%d ", L->Data[i]);
    }
    printf("\n");
    return 0;
}

 

输出:

9 8 7 6 5 4 3 2 1 10 11 12 13
请按任意键继续. . .

 链式存储:

/*!
 * \file 线性表的链式存储实现.cpp
 *
 * \author ranjiewen
 * \date 三月 2017
 *
 * 
 */
#include <stdio.h>
#include <stdlib.h>

typedef struct LNode *PtrToNode;
typedef int ElemType;
struct LNode
{
    ElemType Data;
    PtrToNode Next;
};

typedef PtrToNode Position;
typedef PtrToNode List;

#define  ERROR NULL

int Length(List ptrL)
{
    List p = ptrL;
    int j = 0;
    while (p)
    {
        p = p->Next;
        j++;
    }
    return j;
}

/*查找*/
Position Find(List L, ElemType x)  //按值查找
{
    Position p = L;/*p指向L的第1个结点*/
    while (p&&p->Data!=x)
    {
        p = p->Next;
    }
    if (p)
    {
        return p;
    }
    else
    {
        return ERROR;
    }
}

List FindKth(int K, List ptrL)//按序号查找
{
    List p = ptrL;
    int i = 1;
    while (p!=NULL&&i<K)
    {
        p = p->Next;
        i++;
    }
    if (i==K)
    {
        return p;
    }
    else
    {
        return NULL;
    }
}

/*插入*/
bool Insert(List L, ElemType x, Position p) //在p的前一节点插入
{
    Position temp, pre;
    for (pre = L; pre&&pre->Next != p; pre = pre->Next); //找到p的前一个结点
    if (pre == NULL)
    {
        printf("插入位置参数错误。\n");
        return false;
    }
    else
    {
        temp = (Position)malloc(sizeof(struct LNode));
        temp->Data = x;
        temp->Next = p;
        pre->Next = temp;
        return true;
    }
}

List Insert(ElemType x, int i, List ptrL) //按序号位置插入
{
    List p, s;
    if (i == 1) //新节点插入在表头
    {
        s = (List)malloc(sizeof(struct LNode));
        s->Data = x;
        s->Next = NULL;
        return s;
    }
    p = FindKth(i - 1, ptrL);
    if (p==NULL)
    {
        printf("参数i错误");
        return NULL;
    }
    else
    {
        s = (List)malloc(sizeof(struct LNode));
        s->Data = x;
        s->Next = p->Next;
        p->Next = s;
        return ptrL;
    }
}

bool Delete(List L, Position p)
{
    Position temp, pre;
    for (pre = L; pre&&pre->Next != p; pre = pre->Next);
    if (pre==NULL||p==NULL)
    {
        printf("删除位置参数错误!\n");
        return false;
    }
    else
    {
        pre->Next = p->Next;
        free(p);
        return true;
    }
}

int main()
{
    List L=NULL;
    for (int i = 1; i < 10;i++)
    {
        L=Insert(9 - i, i, L);
    }
    //Delete(L, );
    printf("链表长度:%d", Length(L));
    return 0;
}

 

posted @ 2017-03-16 23:57  ranjiewen  阅读(674)  评论(0编辑  收藏  举报