線性表 | 靜態鏈表

前言

靜態鏈表,是通過遊標來記錄下一個節點的位置,約定第一個遊標指向備用列表的頭,最後一個遊標指向靜態鏈表的頭,比如:
在這裏插入圖片描述

Don’t talk. Show me the code

  • 頭文件
#ifndef _STATIC_LINK_LIST_H_
#define _STATIC_LINK_LIST_H_

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 100

typedef int ElemType;
typedef int status;

typedef struct 
{
    /* data */
    ElemType data;
    int cur;
}static_list_t,static_list[MAXSIZE];

//函數聲明
status static_list_init(static_list space);
status search_bp_positon(static_list space);
status static_list_insert(static_list space,int p,ElemType e);
status print_static_link_list(static_list space);
int static_list_length(static_list space);
status delete_element(static_list space,int p,ElemType *e);

#endif
  • c文件
#include <stdio.h>
#include "static_link_list.h"


int main()
{
    int e = 0;
    static_list array;
    //初始化靜態鏈表
    static_list_init(array);

    //插入元素
    static_list_insert(array,1,2);
    static_list_insert(array,1,3);
    static_list_insert(array,2,4);
    printf("The length of the list is %d.\n",static_list_length(array));
    printf("Print the list: ");
    print_static_link_list(array);

    //刪除元素
    delete_element(array,2,&e);
    printf("The deleted element is %d.\n",e);
    printf("The length of the list is %d.\n",static_list_length(array));
    printf("Print the list: ");
    print_static_link_list(array);
}


/*func:Initializes the static link list.
  para:space:struct array
  return:
    success:OK
    fail:ERROR
*/
status static_list_init(static_list space)
{
    int i = 0;

    for(i = 0;i < MAXSIZE-1;i++)
    {
        space[i].cur = i+1;
    }
    space[MAXSIZE-1].cur = 0;//最後一個遊標指向0

    return OK;
}

/*func: Find a backup location.
  para:space:struct array
  return:
    success:return backup location
    fail:ERROR
*/
status search_bp_positon(static_list space)
{
    int j;
    j = space[0].cur;
    space[0].cur = space[j].cur;
    return j;
}

/*func: Get the length of the static list.
  para:space:struct array
  return:
    success:Returns the length of a static list.
    fail:ERROR
*/
int static_list_length(static_list space)
{
    int head;
    int k = MAXSIZE-1;
    int count = 0;

    if(space[k].cur)
    {
        head = space[MAXSIZE-1].cur;
        while(head)
        {
            head = space[head].cur;
            count++;
        }
    }
    else
    {
        return 0;
    }

    return count;
}


/*func: Insert the element before the i-th node.
  para:space:struct array
       p:insert positon
       e:insert data
  return:
    success:OK
    fail:ERROR
*/
status static_list_insert(static_list space,int p,ElemType e)
{
    int i;
    int k = MAXSIZE-1;
    int bp;//備用位置
    if(p < 1 || p > static_list_length(space)+1)
    {
        printf("The %d th position exceeds the list length!\n",p);
        return ERROR;
    }

    bp = search_bp_positon(space);
    space[bp].data = e;

    for(i = 1; i <= p-1;i++)
    {
        k = space[k].cur;
    }
    space[bp].cur = space[k].cur;
    space[k].cur = bp;

    return OK;
}

/*func: Print the element from static link list.
  para:space:struct array
  return:
    success:OK
    fail:ERROR
*/
status print_static_link_list(static_list space)
{
    int head;
    int k = MAXSIZE-1;

    if(space[k].cur)
    {
        head = space[k].cur;
        while(head)
        {
            printf("%d ",space[head].data);
            head = space[head].cur;
        }
        printf("\n");
    }
    else
    {
        printf("Static link list is empty!\n");
    }

    return OK;
}

/*func: Delete the element from static link list.
  para:space:struct array
       p    :Delete position
       e    :Returns the element to be deleted.
  return:
    success:OK
    fail:ERROR
*/
status delete_element(static_list space,int p,ElemType *e)
{
    int k = MAXSIZE -1;
    int i;

    if(p < 1 || p > static_list_length(space)+1)
    {
        printf("The %d th position exceeds the list length!\n",p);
        return ERROR;
    }
    //找到刪除結點的前一個結點
    for(i = 1; i <= p-1;i++)
    {
        k = space[k].cur;
    }
    //更改備用列表
    i = space[k].cur;
    space[k].cur = space[0].cur;
    space[0].cur = k;
    //更改遊標
    *e = space[i].data;
    space[k].cur = space[i].cur;
    return OK;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章