数据结构――栈与队列

题目:

1.编写函数,采用链式存储实现栈的初始化、入栈、出栈操作

2.编写函数,采用顺序存储实现栈的初始化、入栈、出栈操作

3.编写函数,采用链式存储实现队列的初始化、入队、出队操作

4.编写函数,采用顺序存储实现队列的初始化、入队、出队操作

5.编写一个主函数,在主函数中设计一个简单的菜单,分别调试上述算法


题目分析:

1.顺序栈的类型定义

#define MAX 100  //栈的最大值
typedef struct
{
    ElemType *base;
    int top;
} SqStack;

2.链栈的类型定义

typedef struct SqNode
{
    SElemType data;
    SqNode *Link;
} *Sqptr;
typedef struct
{
    Sqptr top;  //栈项指针
}

3.顺序队列的类型定义

#define MAX 100 //队列的最大长度
typedef struct
{
    ElemType *base;
    int front,rear;
} SqQueue;

4.单链队列的类型定义

typedef struct QNode
{
    QElemType data;
    struct QNode *next;
} *QueuePtr;
 
typedef struct
{
    QueuePtr frout;   //队头指针
    QueuePtr rear;    //队尾指针
}


下面是具体的程序:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
 
#define OVERFLOW -2 
#define ERROR 0 
#define OK 1 
#define MAX 100    //栈的最大值
 
typedef int SElemType; 
typedef int QElemType; 
typedef struct
{
    SElemType *base;   
    SElemType *top; 
}SqStack;  


SqStack InitStacka( )  //顺序存储实现栈的初始化
{ 
    SqStack S;  
    S.base = ( SElemType * )malloc( MAX *sizeof( SElemType ));  
    if( !S.base ) 
    exit( OVERFLOW  );  
    S.top = S.base;  
return(S); 
} 
 
 
void Pusha(SqStack &S, int x)   //顺序存储实现栈的入栈操作
{
    if(S.top - S.base >= MAX) 
    {
       exit(OVERFLOW);
    }  
    *S.top++ = x; 
}  
 
 
void Popa(SqStack &S)    //顺序存储实现栈的出栈操作
{
    SElemType *p;  
    int x;  
    if(S.top == S.base)
   {
       return ; 
    } 
    else 
   {
       p = S.top;  
       x = * --S.top;  
       printf("\t删除的栈顶元素是%d\n\t出栈操作完成后的栈为:\n", x);  
   } 
} 
 
 
void printa(SqStack S)   //输出
{
    SElemType *p;  
    p = S.base; 
    printf("\t");  
    while(p != S.top)  
   {
       printf("%d ", *(p++));
    } 
    printf("\n"); 
}  
 
 
typedef struct SqNode
{
     SElemType data;  
     SqNode *Link; 
}*Sqptr,NODE; 

typedef struct 
{
     Sqptr top; 
}Stack;

Stack InitStackb()  //链式存储实现栈的初始化
{ 
    Stack S;  
    S.top = ( Sqptr )malloc( sizeof( NODE ) );  
    if( !S.top )
    { 
        exit (OVERFLOW);  
    }
        S.top -> Link = NULL;  
return(S); 
}  
 
 
void Pushb(Stack &S, int x)  //链式存储实现栈的入栈操作
{ 
    Sqptr p;  
    p = ( Sqptr )malloc( sizeof( NODE ) );  
    if( !p ) 
    {
        return ; 
     } 
    p -> data = x;  
    p -> Link = S.top -> Link;  
    S.top -> Link = p; 
}
 
 
void Popb(Stack &S)   //链式存储实现栈的出栈操作
{ 
    int x;  
    Sqptr p;  
    if(S.top -> Link == NULL)
    { 
        return ;  
     }
    else 
   {    
        p = S.top -> Link;        
        x = p -> data;     
        S.top -> Link = p -> Link;     
        printf("\t删除的栈顶元素是%d\n", x);     
       free(p);
    } 
}  
 
typedef struct QNode 
{
    QElemType data;  
    struct QNode *next; 
}*QueuePtr,QNode; 

typedef struct 
{
    QueuePtr front;  
    QueuePtr rear; 
}LinkQueue;

LinkQueue InitQueue()  //链式存储实现队列的初始化
{ 
    LinkQueue Q;  
    Q.front = Q.rear = ( QueuePtr )malloc( sizeof( QNode ) );  
    if( !Q.front ) 
   {
       exit (OVERFLOW); 
    } 
       Q.front -> next = NULL;   
return(Q); 
} 
 
void EnQueue(LinkQueue &Q, QElemType x)   //链式存储实现队列的入队操作
{ 
    QueuePtr p;  
    p = ( QueuePtr )malloc( sizeof( QNode ) );  
    if( !p ) 
    {
        exit(OVERFLOW);  
     }
     p -> data = x; 
     p -> next = NULL; 
     Q.rear -> next = p;  
     Q.rear = p; 
} 
 
 
void DeQueue(LinkQueue &Q)   //链式存储实现队列的出队操作
{ 
    int x;  
    if(Q.front == Q.rear) 
   {
       return ; 
    } 
    QueuePtr p;  
    p = Q.front -> next;  
    x = p -> data;  
    printf("\t删除的队头元素是%d\n", x);  
    Q.front -> next = p -> next;  
    if(Q.rear == p)
    {
        Q.rear = Q.front; 
     }
    free(p);  
return; 
}  
 
typedef struct 
{
    SElemType *base;  
    int front, rear; 
}SqQueue; 

SqQueue InitQueueb()  //顺序存储实现队列的初始化
{ 
    SqQueue S;  
    S.base = ( SElemType * )malloc( MAX *sizeof( SElemType ));  
    if( !S.base ) 
    {
         exit(OVERFLOW);
     }  
    S.front = S.rear = 0;  
return(S); 
}
 
 
void EnQueueb(SqQueue &S, int x)   //顺序存储实现队列的入队操作
{ 
    if((S.rear+1) % MAX == S.front)
    { 
        return ;  
     }
    S.base[S.rear] = x;  
    S.rear = (S.rear+1) % MAX; 
}
 
 
void DeQueueb(SqQueue &S)  //顺序存储实现队列的出队操作
{
    int x;  
    if(S.front == S.rear)
    { 
       return ;  
     }
     x = S.base[S.front];  
     S.front = (S.front+1) % MAX;  
     printf("\t删除的队头元素是:%d\n", x); 
} 
 
 
int main() 
{
    int choice;  
    int n, x;  
    printf("\t1.使用链式存储实现栈的初始化、入栈、出栈操作\n"); 
    printf("\t2.使用顺序存储实现栈的初始化、入栈、出栈操作\n"); 
    printf("\t3.使用链式存储实现队列的初始化、入栈、出栈操作\n"); 
    printf("\t4.使用顺序存储实现队列的初始化、入栈、出栈操作\n"); 
    printf("\t请选择:");  
    scanf("%d", &choice);  
    switch(choice) 
     {
         case 1:
               Stack Sa;
               printf("\t1.链式存储实现栈的初始化\n");     
               printf("\t2.链式存储实现栈的入栈操作\n");     
               printf("\t3.链式存储实现栈的出栈操作\n");           
               while(1)
               {                
                   printf("\t请选择:");                
                   scanf("%d", &n);                
                   switch(n)                
                   {
                        case 1:
                            Sa = InitStackb();                        
                            printf("\t链式存储栈的初始化完成!\n");
                            break;                 
                        case 2:
                            printf("\t以'0'结束\n");
                            printf("\t");         
                            scanf("%d", &x);                     
                            while(x)
                            {               
                                Pushb(Sa, x);
                                scanf("%d", &x);
                             }                  
                            printf("\t链式存储栈的入栈操作完成!\n");
                            break;                 
                        case 3:
                            Popb(Sa);
                            break;
                   }
              }
              break;
         case 2:
              SqStack S;     
              printf("\t1.顺序存储实现栈的初始化\n");     
              printf("\t2.顺序存储实现栈的入栈操作\n");     
              printf("\t3.顺序存储实现栈的出栈操作\n");     
              while(1)
              { 
                  printf("\t请选择:");      
                  scanf("%d", &n);     
                  switch(n)      
                  {  
                      case 1:
                           S = InitStacka();             
                           printf("\t顺序存储栈的初始化完成!\n");
                           break;      
                      case 2:
                           printf("\t以'0'结束!\n");          
                           printf("\t"); 
                           scanf("%d", &x);             
                           while(x)
                           {                      
                                Pusha(S, x);                   
                                scanf("%d", &x);
                           }                   
                           printf("\t顺序存储栈的入栈操作完成!\n");                   
                           printa(S);
                           break;               
                       case 3:
                           Popa(S);         
                           printa(S);
                           break;
                   }
              }
              break;
         case 3:
              LinkQueue Q;         
              printf("\t1.链式存储实现队的初始化\n");          
              printf("\t2.链式存储实现队的入栈操作\n");          
              printf("\t3.链式存储实现队的出栈操作\n");         
              while(1)
              {         
                  printf("\t请选择:");          
                  scanf("%d", &n);          
                  switch(n)          
                  {    
                      case 1:
                           Q = InitQueue();                      
                           printf("\t链式存储队的初始化完成!\n");
                           break;               
                      case 2:
                           printf("\t以'0'结束\n");
                           printf("\t");
                           scanf("%d", &x);                   
                           while(x)
                           {            
                                EnQueue(Q, x);
                                scanf("%d", &x);
                            }                
                           printf("\t链式存储队的入栈操作完成!\n");
                           break;                
                      case 3:
                           DeQueue(Q);
                           break;
                  }
              }
              break;   
         case 4:
              SqQueue Sv;   
              printf("\t1.顺序存储实现队的初始化\n");          
              printf("\t2.顺序存储实现队的入栈操作\n");          
              printf("\t3.顺序存储实现队的出栈操作\n");          
              while(1)
              {          
                  printf("\t请选择:");          
                  scanf("%d", &n);          
                  switch(n)           
                  {       
                      case 1:
                          Sv = InitQueueb();                   
                          printf("\t链式存储栈的初始化完成!\n");
                          break;            
                      case 2:
                          printf("\t以'0'结束\n");
                          printf("\t");
                          scanf("%d", &x);                 
                          while(x)
                          {           
                               EnQueueb(Sv, x);
                               scanf("%d", &x);
                          }              
                          printf("\t链式存储栈的入栈操作完成!\n");
                          break;
                      case 3: 
                          DeQueueb(Sv);
                          break;
                   }
              }
              break; 
        } 
system("pause");
return 0;
}



你可能感兴趣的:(数据结构,链式存储,顺序存储,入栈,出栈,栈与队列)