https://www.cnblogs.com/longhai3/longhai

数据结构>>练习X4

Posted on 2022-02-12 22:06  凡是过去,皆为序曲  阅读(29)  评论(0编辑  收藏  举报

(一个指针)循环链队6个基本

#include<stdlib.h>
#include<stdio.h>
typedef char elemType;
typedef struct node {
elemType data; // 数据域
struct node *next; // 指针域
} *QNode; // 链队数据结点
QNode s;

//初始化
void initQueue(QNode &Q) {
Q=(QNode)malloc(sizeof(node));//带头结点
if(!Q)
exit(0);//分配不成功
Q=NULL;
}

//入队
void EnQueue(QNode &Q,elemType x) {
QNode s;
s=(QNode)malloc(sizeof(QNode));
s->data=x; // 创建存放x 的结点s
if (Q==NULL) { // 原为空队
Q=s;
Q->next=Q; // 构成循环单链表
}
else { // 原队不 空, 结点s 插到 队尾
s->next=Q->next;
Q->next=s;
Q=s;
}
}

//出队
void DeQueue(QNode &Q,elemType &x) {
QNode s;
if (Q==NULL)
printf("队空\n");
if (Q->next==Q) { // 原队只有一个结点
x=Q->data;
free(Q);
Q=NULL;
} else { // 原队有两个或以上 的结点
s=Q->next; // 将Q 之后的结点s 删除
x=s->data;
Q->next=s->next;
free(s);
}
}

//取队头
void GetHead(QNode Q, elemType &e) {
QNode p;
if(Q==NULL)
printf("队空\n");
else {
p = Q->next;
e = p->data;
}
}

//判空
void QueueEmpty(QNode Q) {
if(Q==NULL)
printf("队空\n");
else
printf("队不为空\n");
}

int main() {
initQueue(s);
elemType x;
printf("输入字符串:\n");
while((x=getchar())!='\n') {
EnQueue(s,x);
}
GetHead(s,x);
printf("队头%c\n",x);
while(s!=NULL) {
DeQueue(s,x);
printf("%c",x);
}
QueueEmpty(s);
return 0;
}

==================================================================================

表达式求值一个括号

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef char elemType;
typedef struct StackNode{
elemType data;// 数据域
struct StackNode *next; //指针域
}StackNode,*StackLink;
StackLink s,OPND,OPTR;//声明一个指针s
// 初始化一个不带头结点的链栈,只有头指针
void InitStack(StackLink &s)
{s=NULL;}
//入栈操作,无需进行判栈满操作
void Push(StackLink &s,elemType e)
{
StackLink p;
p=(StackLink)malloc(sizeof(StackNode));
if(!p)
exit(0);
p->data=e;
p->next=s;
s=p;
}
//出栈操作,判断栈是否为空
void Pop(StackLink &s,elemType &x)
{ StackLink p;
if(s!=NULL)
{
x=s->data;//printf("%c" ,s->data);
p=s;
s=s->next;
free(p);
}
}
//取栈项
elemType GetTop(StackLink s)
{ if(s!=NULL)
{ char x=s->data;
return x;}
else
printf("空栈\n");
}
//判断是否运算符
int In(elemType x)
{
if(x!='+'&&x!='-'&&x!='*'&&x!='/'&&x!='('&&x!=')'&&x!='='&&x!='#')
return 1;
else
return 0;
}
//比较预算优先级
elemType Precede(elemType a,elemType b)
{
if(a>b)
return '>';
else if(a<b)
return '<';
else if(a=b)
return '=';
else
printf("错误\n");
}
//部分计算
int Operate(int a,elemType theta,int b)
{
int x;
if(theta=='+')
x=a+b;
else if(theta=='-')
x=a-b;
else if(theta=='*')
x=a*b;
else if(theta=='/')
x=a/b;
else
printf("错误\n");
return x;
}
//求值
char EvaluateExpression() {
InitStack(OPND);//非运算符
InitStack(OPTR);
Push(OPTR,'#');
elemType ch,theta,a,b,x;
while((ch=getchar())!='\n')
{

while(ch!='#'||GetTop(OPTR)!='#') {
if(!In(ch)) {
Push(OPND,ch);
ch=getchar();
}
else
switch(Precede(GetTop(OPTR),ch)) {
case '<':
Push(OPTR,ch);
ch=getchar();
break;

case '>':
Pop(OPTR,theta);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
case '=':
Pop(OPTR,x);
ch=getchar();
break;
}
}
}
return GetTop(OPND);
}

int main()
{
printf("%c",EvaluateExpression());
return 0;
}

 

==================================================================================

 抽奖系统

 

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#define MAX_NUM 999
//定义抽奖人
typedef struct Person {
char name[20];//姓名
char telNo[15];//电话号码
char award;//是否获奖
} Person;
int num=0;//
FILE *fp;//
struct Person Persons[MAX_NUM];//
int awarder[5]= {-1,-1,-1,-1,-1}; //
void readdata() {
int i=0;
struct Person person;//
fp=fopen("D:\\ZHLKS\\dataLucky","r");
if(fp==NULL) {
printf("打开文件D:\\ZHLKS\\dataLucky失败!\n");
return;
}
//
while(!feof(fp)) {
num++;//总人数
fscanf(fp, "%s", person.telNo);
fscanf(fp, "%s", person.name);
person.award='F';//
Persons[i++]=person;
}
}
//
void init() {
int i;
for(i=0; i<num; i++) {
Persons[i].award='F';
}
}
//
void printAwarder(int i) {
printf("姓名: %s\t 手机号码: %s\n", Persons[i].name,Persons[i].telNo);
}
//
void scrolling(struct Person *p,int type) {
int i;
printf("\n\n\n*******************************************\n");
printf("正在抽%d等奖,按回车结束",type);
while(!kbhit()) { //新函数
for(i=0; i<num; i++) {
printf("正在抽%d等奖,按回车结束(仅按一次,不要多按)",type);
printf("\n\n\n*******************%s************************",p[i].name,p[i].telNo);
Sleep(200);//
system("cls");//清屏
}
}
getchar();
}
//
void findAwarder(int *awarder,int n) {
int i;
for(i=0; i<n; i++) {
//
do {
awarder[i]=rand()%num;
} while(Persons[awarder[i]].award=='T');
Persons[awarder[i]].award='T';//
printAwarder(awarder[i]);//
}
}
int main() {
char again='Y';
int type;//
//
readdata();
printf("恭喜您激活本抽奖系统!\n");
srand(time(0));//
while(again=='Y'||again=='y') {
//
init();
printf("\n开始抽一等奖(1名),按回车键开始...\n");
type=1;
getchar();
scrolling(Persons,type);
printf("一等奖获得者: \n");
findAwarder(awarder,1);
printf("\n恭喜上述获奖者\n");
printf("\n开始抽二等奖(2名),按回车键开始...\n");
getchar();
type=2;
scrolling(Persons,type);
printf("二等奖获得者: \n");
findAwarder(awarder,2);
printf("\n恭喜上述获奖者\n");
printf("\n开始抽三等奖(5名),按回车键开始...\n");
getchar();
type=3;
scrolling(Persons,type);
printf("三等奖获得者: \n");
findAwarder(awarder,3);
printf("\n恭喜上述获奖者\n");
printf("\n是否重新开始抽奖?(Y or N)...\n");
again=getchar();
}
return 0;
}

 

==================================================================================

 搜的答案队列扩容

 

#include<stdlib.h>
#include<stdio.h>
#define MAXSIZE 5
#define INCREMENT 5
typedef char elemType;
typedef struct {
elemType *data;//动态 分配内存
int front;//队头
int rear;//队尾
int size;
}seqQueue;
seqQueue s;//此时s不是一个指针变量,是一个普通的结构体类型的变量 s

//初始化
void initQueue(seqQueue &s)
{
s.data=(elemType*)malloc(sizeof(elemType)*MAXSIZE);
if(!s.data)
exit(0);
s.front=s.rear=0;
s.size=MAXSIZE;
}

//入队
void InQueue(seqQueue &s,elemType e)
{
if(((s.rear+1)%s.size)==s.front)
{ s.data=(elemType*)realloc(s.data,sizeof(elemType)*(s.size+INCREMENT));
if(!s.data)
exit(0);
s.rear=s.size;
s.size=s.size+INCREMENT;
// if (s.rear<s.front)
// { int i=s.front;
// int j=0;
// char temp[s.size];
// for (i;i!=s.rear;j++)
// { temp[j] =s.data[i];
// i = (i + 1) % (s.size-INCREMENT);
// }
// for (int k=0; k<(s.size-1-INCREMENT); k++)
// { s.data[k]=temp[k]; }
// s.data[s.size-1-INCREMENT]=e;
// s.front=0;
// s.rear=s.size-1;
// }
}
s.data[s.rear]=e;
s.rear=(s.rear+1)%s.size;
}

//出队
void OutQueue(seqQueue &s,elemType &x)
{
if(s.rear==s.front )
printf("队空,无法出队! \n");
else
{ x=s.data[s. front];
s.front=(s.front+1)%s.size;
}

}

//取队头
elemType getHead(seqQueue s)
{ if(s.rear!=s.front)
return s.data[s.front];
}

//判空
void KQueue(seqQueue s)
{ if(s.rear==s.front )
printf("队空 \n");
else
printf("不是空队 \n");
}

int main()
{
char e,x;
initQueue(s);
while((e=getchar())!='\n')
{ InQueue(s,e);}
OutQueue(s,x);
printf("%c\n",x);
OutQueue(s,x);
printf("%c\n",x);
while((e=getchar())!='\n')
{ InQueue(s,e);}
OutQueue(s,x);
printf("%c\n",x);
while((e=getchar())!='\n')
{ InQueue(s,e);}
printf("队头:%c\n",getHead(s));
KQueue(s);
while(s.front!=s.rear)
{
OutQueue(s,x);
printf("%c",x);
}
KQueue(s);
return 0;
}

 

/*队列也是一种线性表,操作受限的线性表,与栈有设么相问点?
栈只能在一端进行插入和删除操作,,
队列可以在两端进行,其中能够进行插入操作,队尾
删除操作,队头
存储:顺序存储和链式存储
一段连续的存储空间,进行顺序存储。数组 和malloc 动态分配

*/

 

随心,随记

https://www.cnblogs.com/w1hg/331817