
数据结构课程设计题目(高手请进,解决后满意有分送)
实现以下功能:
①创建一个空的链式队列;
②判断新建的链式队列是否为空;
③提示用户输入“链式队列的长度”;
④根据用户输入的“链式队列的长度”,逐一提示
用户输入“链式队列中的各个元素”,完成链式
队列的构造;
⑤判断此时的链式队列是否为空;
⑥调用子程序打印当前链式队列的队首元素和队尾
元素;
⑦编写打印链式队列元素的子程序PrintNode( );
⑧清空队列,连续3次做下列操作:入队4个元素,
出队2个元素,打印链式队列,打印当前链式队列
的队首元素和队尾元素;
⑨要求:提供尽可能友好的人机对话界面,便于用
户(非程序设计者)使用 展开
#include "stdio.h"
typedef int ElemType;
/* 链队列结点结构 */
typedef struct node
{
ElemType data;
struct node *next;
} LNode;
/* 链队列结构 */
typedef struct Queue
{
LNode *front;
LNode *rear;
} Queue;
Queue *InitQueue()
{
Queue *temp;
temp = (Queue *)malloc(sizeof(Queue));
temp->front = (LNode *)malloc(sizeof(LNode));
temp->rear = temp->front;
return temp;
}
/* 判断链队列是否为空 */
int IsEmpty(Queue *Q)
{
return (Q->front==Q->rear ? 1 : 0);
}
/* 将值为data的结点加入到链队列中 */
void EnQueue(Queue *Q, ElemType data)
{
LNode *temp = (LNode *)malloc(sizeof(LNode));
temp->data = data;
temp->next = NULL;
Q->rear->next = temp;
Q->rear = temp;
}
/* 从链队列中移出元素,保存在data中 */
void DeQueue(Queue *Q, ElemType *data)
{
LNode *temp;
if(IsEmpty(Q))
{
printf("Error! Queue is empty.");
return;
}
temp = Q->front->next;
*data = temp->data;
Q->front->next = temp->next;
if(temp == Q->rear)
{
Q->rear = Q->front;
}
free(temp);
}
/* 创建size个结点的链队列 */
void CreateQueue(Queue *Q, int size)
{
ElemType data;
int i;
for(i=0; i<size; i++)
{
printf("Element %d : ", i+1);
scanf("%d", &data);
EnQueue(Q, data);
}
}
/* 取链队列的队首元素 */
ElemType GetFirstNode(Queue *Q)
{
return (IsEmpty(Q) ? NULL : Q->front->next->data);
}
/* 取链队列的队尾元素 */
ElemType GetLastNode(Queue *Q)
{
return (IsEmpty(Q) ? NULL : Q->rear->data);
}
/* 清空链队列 */
void Clear(Queue *Q)
{
LNode *temp, *p;
if(IsEmpty(Q))
{
return;
}
for(temp=Q->front->next; temp!=NULL;)
{
p = temp;
temp = temp->next;
free(p);
}
Q->rear = Q->front;
}
/* 输出链队列全部结点的值 */
void PrintNode(Queue *Q)
{
LNode *curr;
if(IsEmpty(Q))
{
printf("Queue is empty!\n");
return;
}
for(curr=Q->front->next; curr!=NULL; curr=curr->next)
{
printf("%d ", curr->data);
}
printf("\n");
}
void main()
{
Queue *Q = InitQueue();
int size;
ElemType data;
int choice;
while(1)
{
system("cls");
printf("***********************************************\n");
printf(" Link Queue Operation Demo\n\n");
printf(" 1. Create queue\n");
printf(" 2. Queue is empty?\n");
printf(" 3. In queue\n");
printf(" 4. Out queue\n");
printf(" 5. Get first element\n");
printf(" 6. Get last element\n");
printf(" 7. List nodes of queue\n");
printf(" 8. Clear queue\n");
printf(" 0. Exit\n\n");
printf("***********************************************\n");
printf(" Your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 0:
{
return;
}
case 1:
{
printf("Input size of queue : ");
scanf("%d", &size);
CreateQueue(Q, size);
break;
}
case 2:
{
if(IsEmpty(Q))
{
printf("Queue is empty!\n");
}
else
{
printf("Queue is not empty!\n");
}
break;
}
case 3:
{
printf("Input the value of queue node : ");
scanf("%d", &data);
EnQueue(Q, data);
break;
}
case 4:
{
DeQueue(Q, &data);
printf("Value of queue node Deleted : %d\n", data);
break;
}
case 5:
{
printf("First element : %d\n", GetFirstNode(Q));
break;
}
case 6:
{
printf("Last element : %d\n", GetLastNode(Q));
break;
}
case 7:
{
printf("All queue nodes : \n");
PrintNode(Q);
break;
}
case 8:
{
Clear(Q);
printf("Clear nodes successfully from queue!\n");
break;
}
default:
{
break;
}
}
system("pause");
}
}
运行界面:
//学编程要勤于练习,这要才能提高,数据结构预算法很重要,自己努力吧
//记着给一个好评
#include"stdio.h"
#include"stdlib.h"
#include"util.h"
#define OK 1;
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;//对头指针
QueuePtr rear;//队尾指针
}LinkQueue;
void input();
Status InitQueue(LinkQueue &Q)
{
Q.front=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)
exit(OVERFLOW);
Q.front->next=NULL;
Q.rear=Q.front;
return OK;
}
Status DestroyQueue(LinkQueue & Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status EnQueue(LinkQueue & Q,QElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{
QueuePtr p;
if(Q.front==Q.rear)
return (ERROR);
p=Q.front->next;
e=p->data;
Q.front->next=Q.front->next->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return OK;
}
void menu()
{
int num;
LinkQueue Q;
printf("\n*************************************************\n");
printf("*************************************************\n");
for(int i=0;i<2;i++)
{
printf("** **\n");
}
printf("** 输入 1 ,进行入队列 输入 2 ,进行出队列 **\n");
printf("** ");
printf("请选择: ");
scanf(" %d",&num);
printf(" **");
for(i=0;i<2;i++)
{
printf("** **\n");
}
printf("*************************************************\n");
printf("*************************************************\n");
switch(num)
{
case 1: input();break;
case 2: DeQueue(Q,num);break;
default:break;
}
}
void input()
{
int e;
LinkQueue Q;
printf("请输入插入对头的元素:");
scanf("%d",e);
EnQueue(Q,e);
}
void main()
{
int i,j,e;
LinkQueue Q;
InitQueue(Q);
printf("输入入队列元素个数:");
scanf("%d",&j);
for(i=0;i<j;i++)
{
printf("输入第%d个入队列元素:",i+1);
scanf("%d",&e);
EnQueue(Q,e);
}
QueuePtr p;
p=Q.front;
while(p->next)
{
printf("%2d",p->next->data);
p=p->next;
}
printf("\n删除对头元素:");
DeQueue(Q,e);
printf("%d\n",e);
p=Q.front;
while(p->next)
{
printf("%2d",p->next->data);
p=p->next;
}
while(Q.front!=Q.rear)
{
menu();
}
printf("此时队列已经为空,无法操作\n");
}