zoukankan      html  css  js  c++  java
  • 《数据结构教程》(李春葆 主编)课后习题【练习题3】

    【3.2.2】

     1 #include <iostream>
     2 using namespace std;
     3 int islegal(char s[])
     4 {
     5     int i,pushnum = 0,popnum = 0;
     6     for(i=0;s[i];i++){
     7         switch(s[i]){
     8         case 'I':    //入栈操作
     9             pushnum++;break;
    10         case 'O':    //出栈操作
    11             popnum++;break;
    12         default:break;
    13         }
    14         if(pushnum<popnum)
    15             return 0;
    16     }
    17     if(pushnum!=popnum)
    18         return 0;
    19     return 1;
    20 }
    21 int main()
    22 {
    23     char s[100];
    24     while(cin>>s){
    25         if(islegal(s))
    26             cout<<"Yes"<<endl;
    27         else 
    28             cout<<"No"<<endl;
    29     }
    30     return 0;
    31 }

    【3.3】

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 #define MaxSize 1000
     4 typedef struct {
     5     char data[MaxSize];
     6     int top;
     7 } SqStack;
     8 void InitStack(SqStack* &s)    //初始化
     9 {
    10     s = (SqStack*)malloc(sizeof(SqStack));
    11     s->top = -1;
    12 }
    13 bool push(SqStack* &s,char e)    //入栈
    14 {
    15     if(s->top==MaxSize-1)    //现在已经满了
    16         return false;
    17     s->top++;
    18     s->data[s->top] = e;
    19     return true;
    20 }
    21 bool pop(SqStack* &s,char &e)    //出栈
    22 {
    23     if(s->top==-1)    //现在栈已经空了
    24         return false;
    25     e = s->data[s->top];
    26     s->top--;
    27     return true;
    28 }
    29 bool ispair(char s[])
    30 {
    31     int i;
    32     char t;
    33     SqStack* q;
    34     InitStack(q);    //初始化栈
    35     for(i=0;s[i];i++){    //遇到前括号入栈,遇到后括号出栈比较
    36         switch(s[i]){
    37         case '(':
    38             if(!push(q,'('))
    39                 return false;
    40             break;
    41         case '[':
    42             if(!push(q,'['))
    43                 return false;
    44             break;
    45         case '{':
    46             if(!push(q,'{'))
    47                 return false;
    48             break;
    49         case ')':
    50             if(!pop(q,t))
    51                 return false;
    52             if(t!='(')
    53                 return false;
    54             break;
    55         case ']':
    56             if(!pop(q,t))
    57                 return false;
    58             if(t!='[')
    59                 return false;
    60             break;
    61         case '}':
    62             if(!pop(q,t))
    63                 return false;
    64             if(t!='{')
    65                 return false;
    66             break;
    67         default:break;
    68         }
    69     }
    70     if(q->top!=-1)
    71         return false;
    72     return true;
    73 }
    74 int main()
    75 {
    76     char a[MaxSize];
    77     while(scanf("%s",a)!=EOF){
    78         if(ispair(a))
    79             printf("Yes
    ");
    80         else 
    81             printf("No
    ");
    82     }
    83     return 0;
    84 }

    【3.5】

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #define MaxSize 4
      4 typedef struct {
      5     char data[MaxSize];
      6     int front,rear;
      7 }SqQueue;
      8 void Init(SqQueue* &q)    //队列初始化
      9 {
     10     q = (SqQueue*)malloc(sizeof(SqQueue));
     11     q->front = q->rear = 0;
     12 }
     13 void push(SqQueue* &q,char e)    //入队列操作
     14 {
     15     if((q->rear+1)%MaxSize==q->front){
     16         printf("循环队列已满!
    ");
     17         return ;
     18     }
     19     q->rear = (q->rear+1)%MaxSize;
     20     q->data[q->rear] = e;
     21 }
     22 void pop(SqQueue* &q)    //出队列操作
     23 {
     24     if(q->rear==q->front){
     25         printf("循环队列已空!
    ");
     26         return ;
     27     }
     28     q->front = (q->front+1)%MaxSize;
     29 }
     30 void print(const SqQueue* &q)    //输出队列
     31 {
     32     int i;
     33     for(i=(q->front+1)%MaxSize;(i-1+MaxSize)%MaxSize!=q->rear;i=(i+1)%MaxSize)
     34         printf("%c ",q->data[i]);
     35     printf("
    ");
     36 }
     37 void invert(SqQueue* &q)    //逆置循环队列
     38 {
     39     if(q->front > q->rear){    //如果队列跨越了0
     40         int i=MaxSize-1,j=q->rear;
     41         for(;i>q->front;i--){    //将0之前的部分放到逆置后的位置
     42             char t;
     43             ++j;
     44             t=q->data[i];q->data[i]=q->data[j];q->data[j]=t; 
     45         }
     46         j = q->rear;
     47         for(i=0;i<j;i++){    //将0之后的部分逆置
     48             char t;
     49             t=q->data[i];q->data[i]=q->data[j];q->data[j]=t;
     50             j--;
     51         }
     52         q->rear = (q->rear-q->front+MaxSize-1)%MaxSize;
     53         q->front = MaxSize-1;
     54     }
     55     else{    //队列在0之后
     56         int i=q->front+1,j=q->rear;
     57         for(;i<j;i++){    //原地逆置
     58             char t;
     59             t=q->data[i];q->data[i]=q->data[j];q->data[j]=t;
     60             j--;
     61         }
     62         j=0;
     63         for(i=q->front+1;i<=q->rear;i++)    //前移
     64             q->data[j++] = q->data[i];
     65         j--;
     66         q->rear = j;
     67         q->front = MaxSize-1;
     68     }
     69 }
     70 int Menu()
     71 {
     72     int in;
     73     printf("[1] 入队列操作
    ");
     74     printf("[2] 出队列操作
    ");
     75     printf("[3] 输出队列
    ");
     76     printf("[4] 逆置循环队列
    ");
     77     printf("[0] 按其他键退出
    ");
     78     scanf("%d",&in);
     79     return in;
     80 }
     81 void Work(SqQueue* &q,int in)
     82 {
     83     char e;
     84     switch(in){
     85     case 1:
     86         printf("你要输入的元素值:
    ");
     87         getchar();
     88         scanf("%c",&e);
     89         push(q,e);
     90         break;
     91     case 2:
     92         pop(q);
     93         break;
     94     case 3:
     95         printf("队列内容:
    ");
     96         print(q);
     97         break;
     98     case 4:
     99         invert(q);
    100         printf("队列逆置结束
    ");
    101         break;
    102     default:
    103         exit(1);
    104     }
    105     system("pause");
    106     system("cls");
    107 }
    108 int main()
    109 {
    110     //循环队列初始化
    111     SqQueue* q;
    112     Init(q);
    113     //菜单
    114     while(1){
    115         int in = Menu();
    116         Work(q,in);
    117     }
    118     return 0;
    119 }

    【3.6】

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 #define MaxSize 1000
     4 typedef struct SqQueue{
     5     int data[MaxSize];
     6     int front,rear;
     7     SqQueue* next;
     8 }SqQueue;
     9 void Init(SqQueue &q)    //队列初始化
    10 {
    11     q.front = -1;
    12     q.rear = -1;
    13     q.next = NULL;
    14 }
    15 bool QueueEmpty(SqQueue q)    //判断队列是否为空
    16 {
    17     if(q.front == q.rear)
    18         return true;
    19     else 
    20         return false;
    21 }
    22 void push(SqQueue &q,int x)    //入队列操作
    23 {
    24     q.rear++;
    25     q.data[q.rear] = x;
    26 }
    27 SqQueue* Link(SqQueue q[])    //队列连接函数
    28 {
    29     int i,pre = -1;
    30     SqQueue* head = (SqQueue*)malloc(sizeof(SqQueue));
    31     for(i=0;i<10;i++){
    32         if(!QueueEmpty(q[i])){    //如果当前队列不为空,连接到结果队列中
    33             if(pre==-1){
    34                 q[i].next = NULL;
    35                 head->next = &q[i];
    36             }
    37             else{    //如果当前队列不是第一个,上一个队列连接到当前队列
    38                 q[i].next = NULL;
    39                 q[pre].next = &q[i];
    40             }
    41             pre = i;
    42         }
    43     }
    44     return head;
    45 }
    46 int main()
    47 {
    48     SqQueue q[10];
    49     int n,i;
    50     for(i=0;i<10;i++)
    51         Init(q[i]);
    52     printf("你要输入多少个数?
    ");
    53     scanf("%d",&n);
    54     printf("请输入数据(0<=i<=9):
    ");
    55     for(i=1;i<=n;i++){    //输入
    56         int t;
    57         scanf("%d",&t);
    58         push(q[t],t);
    59     }
    60     SqQueue* head = Link(q);
    61     head = head->next;
    62     printf("连接之后的链表内容:
    ");
    63     while(head){
    64         for(i = head->front+1;i<=head->rear;i++)
    65             printf("%d ",head->data[i]);
    66         head = head->next;
    67         printf("
    ");
    68     }
    69     return 0;
    70 }

     

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    poj 2560Freckles (krusual)
    ACRush 楼天成回忆录
    大腕版ACMICPC比赛
    POJ刷题
    DataGrid中添加DropdownList时的数据绑定
    【转帖】SQL Server各种日期计算方法(收藏)
    安全配置Win2000服务器
    C#写的一个代码生成器
    .Net 常用加密算法类
    实习之最
  • 原文地址:https://www.cnblogs.com/yym2013/p/3650632.html
Copyright © 2011-2022 走看看