验证性实验三 栈、队列的实现及应用

来源:互联网 发布:linux查看网卡状态 编辑:程序博客网 时间:2024/05/21 09:27

一、    实验目的

1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。

2、掌握栈和队列的特点,即先进后出与先进先出的原则。

3、掌握栈和队列的基本操作实现方法。

二、    实验任务

1.    实现栈的顺序存储

2.    利用栈实现数制转换

—————————————————————————————————

实验内容:

任务一:实现栈的顺序存储

代码如下:

 

#include "stdio.h"

 

# define MAXSIZE 100

 

#include"malloc.h"

  typedef int ElemType;

typedef struct

{

   ElemType data[MAXSIZE];

   int top;

}SeqStack;

//构造一个空栈

int InitStack(SeqStack *s)

{

   s->top=0;

  return 1;

}

//判断栈是否为空

int StackEmpty(SeqStack *s)

{

   if(s->top==0)  return 1;

   else return 0;

}

int StackFull(SeqStack *s)

{ if(s->top==MAXSIZE-1)  return 1;

   else return 0;

}

//插入新元素x为栈顶元素

int Push(SeqStack *s,int x)

{

   if (StackFull(s))

{  

   printf("the stack is overflow!\n");

   return 0;

}

  else {  s->data[s->top]=x;

s->top++;

}

}

void display(SeqStack *s)

{

   if(s->top==0)

    printf("the stack is empty!\n");

  else

  {

     while(s->top!=0)

     {

        printf("%d->",s->data[s->top-1]);

            s->top=s->top-1;

     }

  }

}

 ElemType Pop(SeqStack *s)

 {   if(StackEmpty(s)) return 0;

     else return s->data[--s->top];

 }

 ElemType  StackTop(SeqStack *s)

 {   int i;

if(StackEmpty(s)) return 0;

     else { i=s->top-1;

return s->data[i];}  /*返回栈顶元素的值,但不改变栈顶指针*/

}

 void main()

 {int n,i,k,h,x1,x2,select;

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

  printf("create a empty stack!\n");

  InitStack(p);

  printf("input a stack length:\n");

  scanf("%d",&n);

  for(i=0;i<n;i++)

  {  printf("input a stack value:\n");

     scanf("%d",&k);

     Push(p,k);

  }

   printf("select 1:Display()\n");

   printf("select 2:Push()\n");

   printf("select 3:Pop()\n");

   printf("select 4:StackTop()\n");

   printf("input a your select(1-4):\n");

   scanf("%d",&select);

   switch(select)

  {

   case 1:

      {  display(p);

         break;

      }

   case 2:

      {  printf("input a push a value:\n");

         scanf("%d",&h);

         Push(p,h);

         display(p);

         break;

      }

   case 3:

      {  x1=Pop(p);

            printf("x1->%d\n",x1);

            display(p);

            break;

        }

   case 4:

      {  x2=StackTop(p);

        printf("x2->%d",x2);

        break;

      }

}

 }

实现栈的顺序存储,完成以下功能:

1.构建一个空栈

 

 

 

 

 

 

 

 

 

 

 

 

 

2.依次输入入栈的值

3.显示输入的值

4.弹出栈顶值:

 

任务二 利用栈实现数制转换

代码如下:

#include "stdio.h"

#include "malloc.h"

# define MAXSIZE 100

typedef  int  ElemType;  /*将顺序栈的元素定义为整型*/

typedef struct

 { ElemType data[MAXSIZE];

   int top;

}SeqStack;

 

 int InitStack(SeqStack *s)

 {s->top=0;

  return 1;

}

int StackEmpty(SeqStack *s)

 { if(s->top==0)  return 1;

   else return 0;

}

int StackFull(SeqStack *s)

 { if(s->top==MAXSIZE-1)  return 1;

   else return 0;

}

int Push(SeqStack *s,int x)

 { if (StackFull(s)){   printf("the stack is overflow!\n");

               return 0;

                 }

  else {  s->data[s->top]=x;

s->top++;

}

 }

 ElemType Pop(SeqStack *s)

 { ElemType y;

   if(StackEmpty(s)){ printf("the stack is empty!\n");

              return 0;

}

  else {  y=s->data[s->top-1];

       s->top=s->top-1;

       return y;

       }

 }

 ElemType  StackTop(SeqStack *s)

 {   if(StackEmpty(s)) return 0;

     else return s->data[s->top];

}

void Dec_to_Ocx (int N)   /* n是非负的十进制整数,输出等值的八进制数*/

{

       /*定义一个顺序栈*/

 

SeqStack *S = (SeqStack *)malloc(sizeof(SeqStack));

ElemType x;                          

InitStack(S);    /*初始化栈*/

if(N<0)

    {

printf("\n错误,输入的数必须大于0");

    return;

   }

  if(!N) Push(S,0);

while(N)         /*自右向左产生八进制的各位数字,并将其进栈*/

      {   Push(S,N%8);     /*余数入栈 */

       N=N/8;            /*商作为被除数*/

     }

 

 

while(!StackEmpty(S))    /*栈非空时退栈输出*/

{    x=Pop(S);

         printf("%d",x);

      }

  printf("\n");

}

 void main( )

 {   

    int n;

  printf("请输入要转换为8进制的数:\n");

scanf("%d",&n);

Dec_to_Ocx (n);

}

 

 

 

实验结果:

输入十进制的数67

转换为八进制后的结果为103

 

 

 

实验总结:通过本次实验,我掌握了栈和队列的顺序存储和链式存储,栈的主要特点是先进后出,而队列是先进先出。实验中我们掌握了空栈的建立,以及如何向空栈中压入值,弹出值等操作,通过这些练习让我对栈的特性有了更深刻的体会.

 

四、教师批阅及成绩:

 

 

 

原创粉丝点击