博文谷

位置:首頁 > 行政公文 > 報告

數據結構實驗報告

報告2.08W

想必學計算機專業的同學都知道數據結構是一門比較重要的課程,那麼,下面是本站小編給大家整理收集的數據結構實驗報告,供大家閱讀參考。

數據結構實驗報告

數據結構實驗報告1

一、實驗目的及要求

1)掌握棧和隊列這兩種特殊的線性表,熟悉它們的特性,在實際問題背景下靈活運用它們。

本實驗訓練的要點是“棧”和“隊列”的觀點;

二、實驗內容

1) 利用棧,實現數制轉換。

2) 利用棧,實現任一個表達式中的語法檢查(選做)。

3) 編程實現隊列在兩種存儲結構中的基本操作(隊列的初始化、判隊列空、入隊列、出隊列);

三、實驗流程、操作步驟或核心代碼、算法片段

順序棧:

Status InitStack(SqStack &S)

{

=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));

if(!)

return ERROR;

=;

ksize=STACK_INIT_SIZE;

return OK;

}

Status DestoryStack(SqStack &S)

{

free();

return OK;

}

Status ClearStack(SqStack &S)

{

=;

return OK;

}

Status StackEmpty(SqStack S)

{

if(==)

return OK;

return ERROR;

}

int StackLength(SqStack S)

{

return ;

}

Status GetTop(SqStack S,ElemType &e)

{

if(>=ksize)

{

=(ElemType *)realloc(,(ksize+STACKINCREMENT)*sizeof(ElemType));

if(!) return ERROR;

=+ksize;

ksize+=STACKINCREMENT;

}

*++=e;

return OK;

}

Status Push(SqStack &S,ElemType e)

{

if(>=ksize)

{

=(ElemType *)realloc(,(ksize+STACKINCREMENT)*sizeof(ElemType));

if(!)

return ERROR;

=+ksize;

ksize+=STACKINCREMENT;

}

*++=e;

return OK;

}

Status Pop(SqStack &S,ElemType &e)

{

if(==)

return ERROR;

e=*;

return OK;

}

Status StackTraverse(SqStack S)

{

ElemType *p;

p=(ElemType *)malloc(sizeof(ElemType));

if(!p) return ERROR;

p=;

while(p!=)//上面一個...

{

p--;

printf("%d ",*p);

}

return OK;

}

Status Compare(SqStack &S)

{

int flag,TURE=OK,FALSE=ERROR;

ElemType e,x;

InitStack(S);

flag=OK;

printf("請輸入要進棧或出棧的元素:");

while((x= getchar())!='#'&&flag)

{

switch (x)

{

case '(':

case '[':

case '{':

if(Push(S,x)==OK)

printf("括號匹配成功!nn");

break;

case ')':

if(Pop(S,e)==ERROR || e!='(')

{

printf("沒有滿足條件n");

flag=FALSE;

}

break;

case ']':

if ( Pop(S,e)==ERROR || e!='[')

flag=FALSE;

break;

case '}':

if ( Pop(S,e)==ERROR || e!='{')

flag=FALSE;

break;

}

}

if (flag && x=='#' && StackEmpty(S))

return OK;

else

return ERROR;

}

鏈隊列:

Status InitQueue(LinkQueue &Q)

{

t ==

(QueuePtr)malloc(sizeof(QNode));

if (!t) return ERROR;

t->next = NULL;

return OK;

}

Status DestoryQueue(LinkQueue &Q)

{

while(t)

{

=t->next;

free(t);

t=;

}

return OK;

}

Status QueueEmpty(LinkQueue &Q)

{

if(t->next==NULL)

return OK;

return ERROR;

}

Status QueueLength(LinkQueue Q)

{

int i=0;

QueuePtr p,q;

p=t;

while(p->next)

{

i++;

p=t;

q=p->next;

p=q;

}

return i;

}

Status GetHead(LinkQueue Q,ElemType &e)

{

QueuePtr p;

p=t->next;

if(!p)

return ERROR;

e=p->data;

return e;

}

Status ClearQueue(LinkQueue &Q)

{

QueuePtr p;

while(t->next )

{

p=t->next;

free(t);

t=p;

}

t->next=NULL;

->next=NULL;

return OK;

}

Status EnQueue(LinkQueue &Q,ElemType e)

{

QueuePtr p;

p=(QueuePtr)malloc(sizeof (QNode));

if(!p)

return ERROR;

p->data=e;

p->next=NULL;

->next = p;

=p; //p->next 爲空

return OK;

}

Status DeQueue(LinkQueue &Q,ElemType &e)