#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <math.h>
#include <sys/timeb.h>
#include <stdarg.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int Boolean;
typedef int ElemTtpe;
//typedef ElemType * Triplet;
/c2-3.h線性表的靜態單鏈表存盤結構。
#define MAX_SIZE 100
typedef struct
{
ElemType data;
int cur;
}component,SLinkList[MAX_SIZE];
/bo2-5.h 靜態鏈表(資料結構由c2-3.h定義)的基本操作(13個)
#define DestroyList ClearList//DestroyList()和ClearList()的操作是一樣的
int Malloc(SLinkList space)
{ //備用鏈表非空,則回傳分配的結點下標(備用鏈表的第一個結點);否則回傳0
int i=space[0].cur;//備用鏈表第一個結點的位置
if(i)
space[0].cur=space[i].cur;
return i;
}
void Free(SLinkList space,int k)
{
//將下標為k的空閑結點回收到備用鏈表中(成為備用鏈表的第一個結點)
space[k].cur=space[0].cur;
space[0].cur=k;
}
void InitList(SLinkList L)
{
//構造一個空的鏈表L,表頭為L的最后一個單元L[MAX_SIZE-1],其余單元鏈成
//一個備用鏈表,表頭為L的第一個單元L[0],"0"表示空指標。
int i;
L[MAX_SIZE-1].cur=0;
for(i=0;i<MAX_SIZE-2;i++)
L[i].cur=i+1;
L[MAX_SIZE-2].cur=0;
}
void ClearList(SLinkList L)
{
//初始條件:線性表已存在。操作結果:將L重置為空表
int j,i=L[0].cur;
while(i)
{
j=i;
i=L[i].cur;
}
L[j].cur=L[MAX_SIZE-1].cur;
L[MAX_SIZE-1].cur=0;
}
Status ListEmpty(SLinkList L)
{
//若L是空表,則回傳TRUE;否則回傳FALSE
if(L[MAX_SIZE-1].cur==0)
return TRUE;
else
return FALSE;
}
int ListLength(SLinkList L)
{
//回傳L中資料元素的個數
int j=0,i=L[MAX_SIZE-1].cur;
while(i)
{
i=L[i].cur;
j++;
}
return j;
}
Status GetElem(SLinkList L,int i,ElemType &e)
{
//用e回傳L中第i個元素的值
int m,k=MAX_SIZE-1;
if(i<1 || i>ListLength(L))
return ERROR;
for(m=1;m<=i;m++)
k=L[k].cur;
e=L[k].data;
return OK;
}
int LocateElem(SLinkList L,ElemType e)
{
//在靜態單鏈線性表L中查找第一個值為e的元素。若找到,則回傳它在L中的位序;否則回傳0
//(與其他LocateElem()的定義不同)
int i=L[MAX_SIZE-1].cur;
while(i && L[i].data!=e)
i=L[i].cur;
return i;
}
Status PriorElem(SLinkList L,ElemType cur_e,ElemType &pre_e)
{
//初始條件:線性表L存在
//操作結果:若cur_e是L的資料元素,且不是第一個,則用pre_e回傳它的前驅;
// 否則操作失敗,pre_e無定義
int j,i=L[MAX_SIZE-1].cur;
do
{
j=i;
i=L[i].cur;
}while(i && cur_e!=L[i].data);
if(i)
{
pre_e=L[j].data;
return OK;
}
return ERROR;
}
Status NextElem(SLinkList L,ElemType cur_e,ElemType &next_e)
{
//初始條件:線性表L已存在
//操作結果:若cur_e是資料L的資料元素,且不是最后一個,則用next_e回傳它的后繼;
// 否則操作失敗,next_e無定義
int j,i=LocateElem(L,cur_e);
if(i)
{
j=L[i].cur;
if(j)
{
next_e=L[j].data;
return OK;
}
}
return ERROR;
}
Status ListInsert(SLinkList L,int i,ElemType e)
{
//在L中第i元素之前插入新的資料元素e
int m,j,k=MAX_SIZE-1;
if((i<1 || i>ListLength(L)+1))
return ERROR;
j=Malloc(L);
if(j)
{
L[j].data=https://bbs.csdn.net/topics/e;
for(m=1;m<i;m++)
k=L[k].cur;
L[j].cur=L[k].cur;
L[k].cur=j;
return OK;
}
return ERROR;
}
Status ListDelete(SLinkList L,int i,ElemType &e)
{
//洗掉在l中第i個數居元素e,并回傳其值
int j,k=MAX_SIZE-1;
if(i<1 || i>ListLength(L))
return ERROR;
for(j=1;j<i;j++)
k=L[k].cur;
j=L[k].cur;
L[k].cur=L[j].cur;
e=L[j].data;
Free(L,j);
return OK;
}
void ListTraverse(SLinkList L,void(*visit)(ElemType))
{
//初始條件:線性表L存在。操作結果:依次對L的每個資料元素呼叫函式visit()
int i=L[MAX_SIZE-1].cur;
while(i)
{
visit(L[i].data);
i=L[i].cur;
}
printf("\n");
}
Status equal(ElemType c1,ElemType c2)
{
//判斷是否相等的函式
if(c1==c2)
return TRUE;
else
return FALSE;
}
int comp(ElemType a,ElemType b)
{
//根據a<、=或>b,分別回傳-1、0或1
if(a==b)
return 0;
else
return(a-b)/abs(a-b);
}
void print(ElemType c)
{
printf(" %d ",c);
}
void print1(ElemType &c)
{
//以十進制整型的格式輸出元素的值
printf(" %d ",c);
}
void print2(ElemType c)
{
//以字符型的格式輸出元素的值
printf(" %c ",c);
}
// func2-3.h檢驗單鏈表基本操作的主函式
//main2-2.cpp、main2-3.cpp.main2-4.cpp和main2-5.cpp呼叫
int main(void)
{
LinkList L;
ElemType e, e0;
Status i;
int j,k;
InitList(L);
for(j=1;j<=5;j++)
i=ListInsert(L,1,j);
printf("在L的表頭依次插入1—5后,L=");
ListTraverse(L,print);
i=ListEmpty(L);
printf("L是否空?i=%d(1:是 0:否),表L的長度=%d\n",i,ListLength(L));
ClearList(L);
//printf("%d\n",ListLength(L));
printf("清空L后,L=");
ListTraverse(L,print);
i=ListEmpty(L);
printf("L是否為空?i=%d(1: 是 0:否),表L的長度=%d\n",i,ListLength(L));
for(j=1;j<=10;j++)
ListInsert(L,j,j);
printf("在L的表尾依次1—10后,L=");
ListTraverse(L,print);
for(j=0;j<=1;j++)
{
#ifdef SLL//僅用于靜態鏈表
k=LocateElem(L,j);
if(k)
printf("值為%d的元素的位序為%d\n",k,j);
#else
k=LocateElem(L,j,equal);
if(k)
printf("第%d個元素的值為%d\n",k,j);
#endif
else
printf("沒有值為%d的元素,",j);
}
for(j=1;j<=2;j++)
{
GetElem(L,j,e0);
i=PriorElem(L,e0,e);
if(i==ERROR)
printf("元素%d無前驅,",e0);
else
printf("元素%d的前驅為%d\n",e0,e);
}
for(j=ListLength(L)-1;j<=ListLength(L);j++)
{ GetElem(L,j,e0);
i=NextElem(L,e0,e);
if(i==ERROR)
printf("元素%d無后繼\n",e0);
else
printf("元素%d的后繼為%d",e0,e);
}
k=ListLength(L);
for(k=k+1;j>=k;j--)
{
i=ListDelete(L,j,e);
if(i==ERROR)
printf("洗掉第%d個元素失敗(存在此元素)。",j);
else
printf("洗掉第%d個元素成功,其值為%d\n",j,e);
}
printf("依次輸出L的元素: ");
ListTraverse(L,print);
DestroyList(L);
#ifndef SLL
printf("銷毀L后,L=%u\n",L);
# endif
return 0;
}
//main2-4.cpp檢驗bo2-5.h的主程式
#include"c1.h"
typedef int ElemType;//定義ElemType為整型
#include"c2-3.h" //靜態單鏈表的存盤結構
#include"bo2-5.h"//靜態單鏈表的基本操作(13個)
#include"func2-2.h"//包括equal()、comp()、print()、print1()和print2()函式
typedef SLinkList LinkList; //定義func2-3.h中的LinkList型別為SLinkList型別
#define SLL//定義了SLL(靜態鏈表標志),使func2-3.h選擇執行靜態鏈表特頭的函式
#include"func2-3.h"//主
uj5u.com熱心網友回復:
單步跟蹤一下吧uj5u.com熱心網友回復:
在vc里怎么單步跟蹤?uj5u.com熱心網友回復:
C++Builder里按F8VC++里按F10
uj5u.com熱心網友回復:
Debug模式下單步F10uj5u.com熱心網友回復:
請在最后return 0;前加入getchar();就可以啦因為程式一直運行到結束,所以就有一開始就停止現象
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/130537.html
標籤:基礎類
上一篇:請問如何動態增加1級選單后,在動態二級選單,有圖片,請進
下一篇:qt中布局位置問題
