练习

3.21
#include<stdio.h>
#include<stdlib.h>
//#define InitLise 10//顺序表
//typedef struct {
// int *data;
// int MaxLise;
// int length;
//}SeqList;
//void InitList(SeqList& L)
//{
// L.data = (int*)malloc(InitLise * sizeof(int));
// L.length = 0;
// L.MaxLise = InitLise;
//}
//void IncreaseSize(SeqList& L, int len)
//{
// int* p = L.data;
// L.data = (int*)malloc((L.MaxLise + len) * sizeof(int));
// for (int i = 0; i < L.MaxLise; i++)
// L.data[i] = p[i];
// L.MaxLise = L.MaxLise + len;
// free(p);
//}
//int main()
//{
// SeqList L;
// InitList(L);
// for (int i = 0; i < 13; i++)
// if (L.length >= L.MaxLise)
// IncreaseSize(L, 5);
// else
// *L.data = i;
// for (int i=0; i < L.MaxLise; i++)
// {
// printf("%d ", L.data[i]);
// }
// return 0;
//}

typedef struct LNode {//单链表
ElemType data;
struct Lnode* next;
}LNode,*LinkList;
bool InitList(LinkList& L) {
L = (LNode*)malloc(sizeof(LNode));
if (L == NULL)
return false;
L->next = NULL;
return true;
}
void test()
{
LinkList L;
InitList(L);
}
int main()
{
test();
return 0;
}

队列练习了解了什么叫“20%时间写代码,80%时间调试”

3.22队列练习
#include<stdio.h>
//#define ElementType int
typedef int ElementType;
#define MaxSize 10//定义队列中元素的最大个数
typedef struct {
ElementType data[MaxSize];//用静态数组存放队列元素
int front, rear;//队头队尾指针
}SqQueue;
void InitQueue(SqQueue &Q){//初始化
Q.front = Q.rear = 0;
}
bool QueueEmpty(SqQueue Q) {
if (Q.front == Q.rear)//判断队空
return true;
else
return false;
}
void import(SqQueue &Q) {//输入
for (int i = 0; i < MaxSize; i++)
{
Q.data[Q.rear] = i;
if (Q.front == ((Q.rear+1)%MaxSize))
{
break;
}
Q.rear = (Q.rear + 1) % MaxSize;
}
}
bool DeQueue(SqQueue& Q,ElementType &y) {//出队
if (Q.rear == Q.front)//判断队空
return false;
y = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
bool EnQueue(SqQueue& Q, ElementType x) {//入队
if ((Q.rear) % MaxSize == Q.front)//判断队满
return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
void print(SqQueue& Q) {//打印
for (int i = 0; i < MaxSize; i++)
{
if(Q.front+i!=Q.rear+1)
printf("%d ", Q.data[Q.front + i]);
}
}
void print2(SqQueue& Q)//打印
{
for (int i = 0; i < MaxSize; i++)
{
if((Q.front+i)%MaxSize!=Q.rear)
printf("%d ", Q.data[Q.front + i]);
}
}
void testQueue() {
SqQueue Q;//声明队列
InitQueue(Q);//初始化队列
QueueEmpty(Q);//判空
import(Q);//输入
int y = 0;
DeQueue(Q,y);//删除队头元素
printf("%d\n", y);
print(Q); //打印
int x = 0;
scanf_s("%d", &x);
EnQueue(Q, x);//入队
print2(Q);//打印
}
int main()
{
testQueue();
return 0;
}