《数据结构》_PTA_数据结构作业2:栈和队列

判断题

1-1
Run the following operations on a stack S: Push(S,1), Push(S,2), Pop(S), Push(S,3), Pop(S), Pop(S). The output sequence must be {1, 2, 3}.
F

1-2
If keys are pushed onto a stack in the order abcde, then it’s impossible to obtain the output sequence cdabe.
T

1-3
序列{1,2,3,4,5}依次入栈,则不可能得到{3,4,1,2,5}的出栈序列。
T

1-4
栈结构不会出现溢出现象。
F

1-5
两个栈共享一片连续空间,可以将两个栈的栈底分别设在这片空间的两端。
T

1-6
Non recursive programs are generally faster than equivalent recursive programs. However, recursive programs are in general much simpler and easier to understand.
T

1-7
Recursive programs are generally faster than equivalent non recursive programs. However, non recursive programs are in general much simpler and easier to understand.
F

1-8
In most restaurants, we follow one principle called “First come, first served”. This principle can be implemented by a stack.
F

1-9
When n elements are pushed into a stack, they may be popped in a different order. But if they are inserted into a queue, they will always be deleted in the same order as the insertions.
T

1-10
栈是后进先出的线性表。
T

1-11
“Circular Queue” is defined to be a queue implemented by a circularly linked list or a circular array.
F

1-12
In a circular queue which is implemented by an array, the front value must always be no larger than the rear value.
F

1-13
In most restaurants, we follow one principle called “First come, first served”. This principle can be implemented by a queue.
T

1-14
循环队列也存在空间溢出的问题。
T

1-15
队列是后进先出的线性表。
F

选择题

2-1
若已知一队列用单向链表表示,该单向链表的当前状态(含3个对象)是:1->2->3,其中x->y表示x的下一节点是y。此时,如果将对象4入队,然后队列头的对象出队,则单向链表的状态是:
2->3->4

2-2
若用大小为6的数组来实现循环队列,且当前front和rear的值分别为0和4。当从队列中删除两个元素,再加入两个元素后,front和rear的值分别为多少?2和0

2-3
现有队列 Q 与栈 S,初始时 Q 中的元素依次是{ 1, 2, 3, 4, 5, 6 }(1在队头),S 为空。若允许下列3种操作:(1)出队并输出出队元素;(2)出队并将出队元素入栈;(3)出栈并输出出栈元素,则不能得到的输出序列是:3, 4, 5, 6, 1, 2

2-4
循环队列的引入,目的是为了克服( 假溢出问题 )。

2-5
循环队列的队满条件为 ( (sq.rear+1) % maxsize ==sq.front )。

2-6
栈和队列的共同点是( 只允许在端点处插入和删除元素 )。

2-7
已知初始为空的队列 Q 的一端仅能进行入队操作,另外一端既能进行入队操作又能进行出队操作。若 Q 的入队序列是 1、2、3、4、5,则不能得到的出队序列是:(4、1、3、2、5)

2-8
有六个元素以6、5、4、3、2、1的顺序进栈,问哪个不是合法的出栈序列?( 3 4 6 5 2 1 )

2-9
若采用带头、尾指针的单向链表表示一个堆栈,那么该堆栈的栈顶指针top应该如何设置?( 将链表头设为top )

2-10
利用大小为n的数组(下标从0到n-1)存储一个栈时,假定栈从数组另一头开始且top==n表示栈空,则向这个栈插入一个元素时,修改top指针应当执行:( top–

2-11
Given the pushing sequence of a stack as {1, 2, 3, 4, 5}. If the first number being popped out is 4, then the last one out must be:(1 or 5

2-12
To delete a node from a linked stack with ST being its top pointer, and save the key value of the deleted node into X, we must do:( X= ST->data; ST = ST->next;

2-13
下列关于栈的叙述中,错误的是:( 仅 1、3、4

  1. 采用非递归方式重写递归程序时必须使用栈
  2. 函数调用时,系统要用栈保存必要的信息
  3. 只要确定了入栈次序,即可确定出栈次序
  4. 栈是一种受限的线性表,允许在其两端进行操作

2-14
《数据结构》_PTA_数据结构作业2:栈和队列_第1张图片
15

2-15
对空栈 S 进行 Push 和 Pop 操作,入栈序列为 a, b, c, d, e,经过 Push, Push, Pop, Push, Pop, Push, Push, Pop 操作后,得到的出栈序列是:(b, c, e

2-16
设有一顺序栈S,元素s1,s2,s3,s4,s5,s6依次进栈,如果6个元素出栈的顺序是s2,s3,s4, s6 , s5,s1,则栈的容量至少应该是( 3 )。

2-17
元素A,B,C,D依次入栈,出栈无限制,则以下(B, A, D, C )是可能的出栈序列。

2-18
用S表示入栈操作,X表示出栈操作,若元素入栈的顺序为1234,为了得到1342出栈顺序,相应的S和X的操作串为( SXSSXSXX )。

2-19
Given the popping sequence of a stack as { a, b, c, d, e, f }. Among the following, the impossible pushing sequence is:( d f e a c b

2-20
Given the popping sequence of a stack as { 1, 2, 3, 4, 5, 6 }. Among the following, the impossible pushing sequence is:( 4 6 5 1 3 2)

程序填空题

实验目的:1、掌握栈和队列的基本知识 2、深入理解栈和队列的特征,掌握并灵活运用栈和队列。3、用顺序结构表示栈并实现栈的各种基本操作 将程序填写完整,实现栈的初始化、释放栈、入栈、出栈等基本操作。

#include
#include
#define MaxSize 100
typedef char ElemType;
typedef struct
{
	ElemType elem[MaxSize];
	int top; //栈指针
} SqStack;
void InitStack(SqStack** s) //初始化栈s
{
	*s = (SqStack*)malloc(sizeof(SqStack));
	(*s)->top = @@ -1 @@;
}
void ClearStack(SqStack* s) //释放栈s
{
	@@ free(s) @@; 
}
int StackLength(SqStack* s) //求栈s长度
{
	int len = 0;
	while (s->top != -1)
	{
		len++;
		s->top--;
	}
	s->top += len;
	return len;
}
int StackEmpty(SqStack* s) //判断栈s是否为空栈
{
	if (@@ s->top == -1 @@)
		return 1;
	else
		return 0;
}
int Push(SqStack* s, ElemType e) //进栈元素e
{
	if (s->top == MaxSize - 1)
		return 0;
	@@ s->top++ @@; 
		s->elem[s->top] = e;
	return 1;
}
int Pop(SqStack* s, ElemType& e) //出栈一个元素
{
	if (s->top == -1)
		return 0;
	else
	{
		@@ e = s->elem[s->top] @@; 
			s->top--;
	}
	return 1;
}
int GetTop(SqStack* s, ElemType* e) //取栈顶元素
{
	if (s->top == -1)
		return 0;
	else
	{
		*e = s->elem[s->top];
	}
	return 1;
}
void DispStack(SqStack* s) //从栈顶到栈底输出元素
{
	while (s->top != -1)
	{
		printf("%c\n", @@ s->elem[s->top] @@);
		s->top--;
	}
}

本题目要求判断顺序栈是否是空栈。

#define MAXSIZE 1024
#include
#include
typedef int datatype;
typedef struct
{
	datatype data[MAXSIZE];
	int top;
}SeqStack;


int Empty_SeqStack(SeqStack* s)
{
	if (@@ s->top == -1 @@) return 1;
	else return 0;
}

本题目要求入顺序栈。

#define MAXSIZE 1024
#include
#include
typedef int datatype;
typedef struct
{
	datatype data[MAXSIZE];
	int top;
}SeqStack;

int Push_SeqStack(SeqStack* s, datatype x)
{
	if (s->top == MAXSIZE - 1) return 0;
	else
	{
		@@ s->top++ @@;
		@@ s->data[s->top] @@ = x;
		return 1;
	}
}

本题目要求出顺序栈。

#define MAXSIZE 1024
#include
#include
typedef int datatype;
typedef struct
{
	datatype data[MAXSIZE];
	int top;
}SeqStack;



int Pop_SeqStack(SeqStack* s, datatype* x)
{
	if (Empty_SeqStack(s)) return 0;
	else
	{
		@@ *x @@ = s->data[s->top];
		@@ s->top-- @@;
		return 1;
	}
}

本题目要求实现压入链栈操作和弹出链栈操作。

#include
#include
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node* next;
}StackNode, * LinkStack;

LinkStack Push_LinkStack(LinkStack top, datatype x)
{
	StackNode* s;
	s = (StackNode*)malloc(sizeof(StackNode));
	s->data = x;
	@@ s->next = top @@;
	@@ top = s @@;
	return top;
}


LinkStack Pop_LinkStack(LinkStack top, datatype* x)
{
	StackNode* p;
	if (top == NULL) return NULL;
	else
	{
		@@ *x = top->data @@;
		p = top;
		@@ top = top->next @@;
		free(p);
		return top;
	}
}

本题目要求Init_SeQueue()函数初始化循环队列,返回指向队列的指针,Empty_SeQueue(c_SeQueue *q)函数判空队列,空队列返回1。否则返回0。

#include 
#include 
#define MAXSIZE 1024
typedef int datatype;
typedef struct {
	datatype data[MAXSIZE]; /*数据的存储区*/
	int front, rear; /*队头队尾指针*/
	int num; /*队中元素的个数*/
}c_SeQueue; /*循环队*/


c_SeQueue* Init_SeQueue()
{
	c_SeQueue* q;
	q = (c_SeQueue*)malloc(sizeof(c_SeQueue));
	@@ q->front = q->rear = 0 @@;
	q->num = 0;
	return q;
}

int Empty_SeQueue(c_SeQueue* q)
{
	if (@@ q->front == q->rear @@) return 1;
	else return 0;
}

本题目要求Out_SeQueue (c_SeQueue *q , datatype *x)函数将 出循环队列的值送给变量x。出队成功返回1,否则返回-1。

#include
#include
#define MAXSIZE 1024
typedef int datatype;
typedef struct {
	datatype data[MAXSIZE]; /*数据的存储区*/
	int front, rear; /*队头队尾指针*/
	int num; /*队中元素的个数*/
}c_SeQueue; /*循环队*/


int Out_SeQueue(c_SeQueue* q, datatype* x)
{
	if (q->num == 0)
	{
		printf("The c+SeQueue is empty");
		return -1;
	}
	else
	{
		q->front = @@ (q->front + 1) % MAXSIZE @@;
		*x = q->data[q->front];
		@@ q->num-- @@;
		return 1;
	}
}

本题目要求Init_LQueue()函数初始化带头结点的链队列,并用Empty_LQueue( LQueue *q)函数判断队列是否为空,队列为空返回1,否则返回0。

#include
#include
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node* next;
}QNode;

typedef struct
{
	QNode* front, * rear;
}LQueue;

LQueue* Init_LQueue()
{
	LQueue* q;
	QNode* p;
	q = (LQueue*)malloc(sizeof(LQueue));
	p = (QNode*)malloc(sizeof(QNode));
	p->next = NULL;
	@@ q->front = q->rear = p @@;
	return q;
}

int Empty_LQueue(LQueue* q)
{
	if (@@ q->front == q->rear @@) return 1;
	else return 0;
}

本题目要求使用函数In_LQueue(LQueue *q , datatype x)将数据x入链队列q中。

#include
#include
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node* next;
}QNode;

typedef struct
{
	QNode* front, * rear;
}LQueue;

void In_LQueue(LQueue* q, datatype x)
{
	QNode* p;
	p = (QNode*)malloc(sizeof(QNode));
	p->data = x;
	p->next = NULL;
	@@ q->rear->next = p @@;
	@@ q->rear = p @@;
}

本题目要求使用函数Out_LQueue(LQueue *q , datatype *x)从链队列q中弹出数据。

#include
#include
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node* next;
}QNode;

typedef struct
{
	QNode* front, * rear;
}LQueue;

int Out_LQueue(LQueue* q, datatype* x)
{
	QNode* p;
	if (Empty_LQueue(q))
	{
		printf("The LQueue is empty");
		return 0;
	}
	else
	{
		p = q->front->next;
		@@ q->front->next = p->next @@;
		*x = p->data;
		free(p);
		if (q->front->next == NULL)
			@@ q->rear = q->front @@;
		return 1;
	}
}

函数merge用于将两个顺序存储的递增有序表合并成一个非递减有序表。如“1,2,3,4”和“2,3,5 ,8,10”合并成“1,2,2,3,3,4,5,8,10”。

#include 
#define M 4
#define N 5

void merge(int a[],int b[],int c[])
{
    int i = 0, j = 0, @@ k=0 @@;
    while (@@ i < M && j < N @@)
    {
        if (a[i] < b[j])
            c[k++] = a[i++];
        else @@ c[k++] = b[j++] @@;
    }
    @@ while (i < M) @@
        c[k++] = a[i++];
    while (j < N)
        c[k++] = b[j++];
}

int main()
{
    int a[M], b[N], c[M + N], i, j;
    for (i = 0; i < M; i++)
        scanf("%d", &a[i]);
    for (j = 0; j < N; j++)
        scanf("%d", &b[j]);
    merge(a, b, c);
    for (i = 0;@@ i<M+N @@; i++)
        printf("%d ", c[i]);
    printf("\n");
    return 0;
}

下列代码的功能是返回带头结点的单链表L的逆转链表。

List Reverse( List L )
{
    Position Old_head, New_head, Temp;
    New_head = NULL;
    Old_head = L->Next;

    while ( Old_head )  {
        Temp = Old_head->Next;
        @@ Old_head->Next = New_head @@;
        New_head = Old_head;
        Old_head = Temp;
    }
    @@ L->Next = New_head @@;
    return L;
}

Concatenation of lists is an operation where the elements of one list are added at the end of another list. For example, if we have a linked list L1→1→2→3 and another one L2→4→5→6. The function ListConcat is to return the head pointer of the list L→1→2→3→4→5→6.

The list structure is defined as the following:

typedef struct Node* PtrToNode;
struct Node {
    int Data;
    PtrToNode Next;
};
typedef PtrToNode List;

Please fill in the blanks.

List ListConcat(List L1, List L2)
{
	List Tmp = L1;
	if (!L1) return L2;
	while (Tmp->Next)
		@@ Tmp = Tmp->Next @@;
	@@ Tmp->Next = L2 @@;
	return @@ L1 @@;
}

你可能感兴趣的:(数据结构复习资料,数据结构,链表,深度优先)