静态链表的所有基本操作

来源:互联网 发布:vb api 编辑:程序博客网 时间:2024/05/20 23:32

头文件:SLinkList.h

#include<iostream>
using namespace std;
#define MAXSIZE 1000
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int ElemType;
typedef int Status;
typedef struct
{
 ElemType data;
 int next;
}component,SLinkList[MAXSIZE];

//基本算法1:将一维数组space中各分量链成一个备用链表,space[0].cur为头指针
void InitSpace_SL(SLinkList& space)
{
 for (int i = 0; i < MAXSIZE - 1; ++i)
  space[i].next = i + 1;
 space[MAXSIZE - 1].next = 0;
}

//基本算法2:若备用空间链表非空,则返回分配的结点下标,否则返回0
int Malloc_SL(SLinkList& space)
{
 int i = space[0].next;
 if (space[0].next)
 {
  space[0].next = space[i].next;
  return i;
 }
 else
  return 0;
}

//基本算法3:将下标为k的空闲结点回收到备用链表
void Free_SL(SLinkList& space, int k)
{
 space[k].next = space[0].next;
 space[0].next = k;
}

//构造一个空的静态链表L
Status InitList_SL(SLinkList& S)
{
 InitSpace_SL(S);
 int L = Malloc_SL(S);
 S[L].next = 0;
 return OK;
}

//销毁静态链表S
Status DestroyList_SL(SLinkList& S)
{
 S[0].next=0;
 return OK;
}

//将静态链表置为空表
Status ClearList_SL(SLinkList& S)
{
 int j,k = S[1].next;
 while (k)
 {
  j = S[k].next;
  Free_SL(S, k);
  k = j;
 }
 S[1].next = 0;
 return OK;
}

//若静态链表为空表,则返回TRUE,否则返回FALSE
Status ListEmpty_SL(SLinkList S)
{
 if (S[1].next)
  return FALSE;
 else
  return TRUE;
}

//返回静态链表S中数据元素的个数
int ListLength_SL(SLinkList S)
{
 int k = 1,length=0;
 while (S[k].next)
 {
  ++length;
  k = S[k].next;
 }
 return length;
}

//用e返回第i个数据元素的值
Status GetElem_SL(SLinkList S, int i, ElemType &e)
{
 if ((i<1) || (i>ListLength_SL(S)))
  return ERROR;
 int k = 1;
 for (int j = 0; (S[k].next )&& (j < i); ++j)
  k = S[k].next;
 e = S[k].data;
 return OK;
}

//返回值为e的元素的位序
int LocateElem_SL(SLinkList S, ElemType e)
{
 int i = S[1].next,position=1;
 while (i&&S[i].data != e)
 {
  ++position;
  i = S[i].next;
 }
 if (S[i].data == e)
  return position;
 else
  return 0;
}

//若cur_e是静态链表S的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
Status PriorElem_SL(SLinkList S, ElemType cur_e, ElemType& pre_e)
{
 int i = S[1].next;
 while (i&&S[i].next)
 {
  int j = S[i].next;
  if (S[j].data == cur_e)
  {
   pre_e = S[i].data;
   return OK;
  }
  i = j;
 }
 return INFEASIBLE;
}

//若cur_e是静态链表S的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
Status NextElem_SL(SLinkList S, ElemType cur_e, ElemType& next_e)
{
 int i = LocateElem_SL(S, cur_e);
 if (i)
 {
  int j = S[i].next;
  if (j)
  {
   next_e = S[j].data;
   return OK;
  }
 }
 return INFEASIBLE;
}

//在静态链表中第i个位置之前插入新的数据元素e,L的长度加1
Status ListInsert_SL(SLinkList& S, int i, ElemType e)
{
 if (i < 1 || i>ListLength_SL(S) + 1)
  return ERROR;
 int j = Malloc_SL(S), p = 1,num=1;
    S[j].data=e;
 while (p && num < i)
 {
  p = S[p].next;
  num++;
 }
 if (!p || num>i)
  return ERROR;
 S[j].next = S[p].next;
 S[p].next = j;
 return OK;
}

//删除静态链表S中第i个数据元素,并用e返回其值
Status ListDelete_SL(SLinkList& S, int i,ElemType& e)
{
 if (i<1 || i>ListLength_SL(S))
  return ERROR;
 int num=0,p =1,q=S[1].next;
 while (q)
 {
  num++;
  if (num == i)
  {
   S[p].next = S[q].next;
   Free_SL(S, q);
   return OK;
  }
  p = q;
  q = S[q].next;
 }
 return ERROR;
}

//打印静态链表S中的数据元素
Status visit(ElemType i)
{
 if (cout << i<<"  ")
  return OK;
 else
  return ERROR;
}
//依次对静态链表L的每个数据元素调用visit().一旦调用失败,则操作失败
Status ListTraverse_SL(SLinkList S, Status(*visit)(ElemType))
{
 int p = S[1].next;
 while (p)
 {
  if (!visit(S[p].data))
   return ERROR;
  p = S[p].next;
 }
 return OK;
}


主函数:

#include"SLinkList.h"
void main(void)
{
 SLinkList S;
 InitList_SL(S);
 for (int i = 1, j = 1; i < 20; ++j, i += 2)
  ListInsert_SL(S, j, i);
 cout << "静态链表S的信息为:" << endl;
 cout << "*******************************************" << endl;
 cout << "长度为:" << ListLength_SL(S) << endl;
 cout << "数据元素分别为:";
 ListTraverse_SL(S, visit);
 cout << endl;
 cout << "*******************************************" << endl;
 ElemType e;
 GetElem_SL(S, 6, e);
 cout << "第6个数据是:" << e << endl;
 ElemType pre;
 if (PriorElem_SL(S, e, pre) != INFEASIBLE)
  cout << e << "前面的数据是:" << pre << endl;
 else
  cout << "数据元素" << e << "没有前驱!" << endl;
 ElemType next;
 if (NextElem_SL(S, e, next) != INFEASIBLE)
  cout << e << "后面的数据是:" << next << endl;
 else
  cout << "数据元素" << e << "没有后继!" << endl;
 cout << "删除数据" << e << endl;
 if (ListDelete_SL(S, 6, e))
 {
  cout << "删除数据成功!" << endl;
  cout << "更新后的数据是:" << endl;
  cout << "*******************************************" << endl;
  cout << "长度为:" << ListLength_SL(S) << endl;
  cout << "数据元素分别为:";
  ListTraverse_SL(S, visit);
  cout << endl;
  cout << "*******************************************" << endl;
 }
 else
  cout << "删除失败!" << endl;
 cout << "值为7的数据元素的位置在:";
 if (LocateElem_SL(S, 7))
  cout << "第" << LocateElem_SL(S, 7) << endl;
 else
  cout << "静态链表中没有此数据元素"<<endl;
 cout << "静态链表S是否为空表:";
 if (ListEmpty_SL(S))
  cout << "是" << endl;
 else
  cout << "否" << endl;
 cout << "现将静态链表S置为空表:" << endl;
 ClearList_SL(S);
 cout << "静态链表S是否为空表:";
 if (ListEmpty_SL(S))
  cout << "是" << endl;
 else
  cout << "否" << endl;
 cout << "将静态链表S销毁!" << endl;
 DestroyList_SL(S);
}

0 0
原创粉丝点击