队列的应用--模拟短信

一、实验目的
本实验是队列的一种典型的应用,队列是一种“先到先服务”的特殊的线性表,本实验要求模拟手机短信功能,使用链式存储结构的队列,进行动态地增加和删除结点信息。
通过本实验的学习,可以理解队列的基本操作的实现。
二、实验内容
设计程序要求,模拟手机的某些短信息功能。
功能要求:
(1)接受短信息,若超过存储容量(如最多可存储20条),则自动将最早接受的信息删除。
(2)显示其中任意一条短信息。
(3)逐条显示短信息。
(4)删除其中的任意一条短信息。
(5)清除。
三、程序分析
采用结构体指针定义存储短信结点:
typedef struct Qnode
{char data[MAXNUM];/字符数组存储短信/
struct Qnode *next;
}Qnodetype; /定义队列的结点/
定义队列:
typedef struct
{ Qnodetype *front;/头指针/
Qnodetype *rear; /尾指针/
int number;/短信数量/
}Lqueue;
(1)int initLqueue(Lqueue **q) 初始化短信队列。
(2)int LInQueue(Lqueue *q,char x[]) 入队列,将字符串x加入到队列尾部。
(3)char * LOutQueue(Lqueue *q) 出队列,删除队头元素,返回其中的字符串。
(4)void get(Lqueue *q,char x[]) 接收短数,若短信数量超过20条,则删除队头短信。
(5)void deleteall(Lqueue *q) 清除所有短信。
(6)void deleteone(Lqueue *q,int n) 删除第n条短信。
(7)void displayall(Lqueue *q) 显示所有短信。
(8)void displayone(Lqueue *q,int n) 显示第n条短信。
在main()函数中,采用菜单方式,菜单中同时显示出已有的短信数量,由用户选择输入命令,实现程序要求功能,命令说明:
R®:接收短信
L(l):显示任意一条短信
A(a):显示所有短信
D(d):删除任意一条短信
U(u):删除所有短信
Q(q):退出
四、程序源代码

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

#define MAXmessage  50
#define LEN    20 
#define TRUE   1
                     /* the storage structure of chain of the queue --by jelly*/
//information node
typedef struct Qnode{
    char data[MAXmessage];
    struct Qnode *next;
}Qnodetype;

//define queue
typedef struct{
    Qnodetype *front;
    Qnodetype *rear;
    int number;
}queue;

//initialization
int initqueue(queue **q){
*q=(queue*)malloc(sizeof(queue));//对queue的变量也要分配空间
(*q)->front=(Qnodetype *)malloc(sizeof(Qnodetype));
(*q)->rear=(*q)->front;
(*q)->front->next=NULL;
(*q)->number=0;
return TRUE;    
}
//in queue
int inqueue(queue *q,char x[]){
 Qnodetype *p;
 int i;
 p=(Qnodetype *)malloc(sizeof(Qnodetype));
 for(i=0;i<MAXmessage;i++) 
 p->data[i]=x[i]; 
 
 p->next=NULL;
 q->rear->next=p;
 q->rear=p;
 (q->number)++;
 return TRUE;
}
//out queue
char *outqueue(queue *q){
Qnodetype *p;
p=q->front->next;
q->front->next=p->next;
if(q->rear==p) q->rear=q->front;
(q->number)--;
return  p->data;
} 
//get information
void get(queue *q, char x[]){
    //(q->number)++;
if((q->number)<LEN){
    inqueue(q,x);
}
else {
outqueue(q);
inqueue(q,x);
}
}
//delete one decided information
void clearone(queue *q,int num){
    int i;
    Qnodetype *m,*n;
    m=n=q->front;
if((q->number)>=num){

  for(i=0;i<=(q->number);i++){
      if(num!=LEN)
{/
      if(i==num-1)
      {
          n=n->next;
          m->next=n->next;
         // free(n);
        //exit(0);    
        break;
      }
      else {
    m=q->front->next ;
    n=m;
         }
}
else 
{
   if(i==num-1)
      {
          n=n->next;
          m->next=NULL;
        //  free(n);
        //exit(0);
        break;    
      }
      else {
    m=q->front->next ;
    n=m;
         }    
}
}
}else printf("error:no this message\n");
(q->number)--;
//exit(0);
}
//delete all
void clearall(queue *q){
while(q->number){
    clearone(q,q->number);    
    }
}
//display one decided information
void displayone(queue *q,int num){
 int i;
 Qnodetype *m;
 m=q->front;
 for(i=0;;i++){
     if(i==num){
     printf("%s\n",m->data);
     break;
     }
      else{
         m=m->next;
     }     
 }        
}
//display all the informations 
void displayall(queue *q){
int i;
Qnodetype *m;
m=q->front;
for(i=0;i<=(q->number);i++){
m=m->next;    
printf("%s\n ",m->data);    
}
}

//
int main() {
queue *q;
Qnodetype *head;
int i;
char command;
char a[MAXmessage];

initqueue(&q);
head=q->front;

while(1){
	
printf("Get information(%d),please enter R/r\n",q->number);
printf("Display one information(%d),please enter L/l\n",q->number);
printf("Display all information(%d),please enter A/a\n",q->number);
printf("Delete one information(%d),please enter D/d\n",q->number);
printf("Delete all information(%d),please enter U/u\n",q->number);
printf("Quit,please enter Q/q\n");
printf("please input command:\n");
scanf("%c",&command);
switch(command)
{case'r':
case'R':gets(a);q->front=head;get(q,a);break;
case'l':
case'L':printf("enter No.:");scanf("%d",&i);q->front=head;displayone(q,i);break;
case'a':
case'A':q->front=head;displayall(q);break;
case'd':
case'D':printf("enter No.:");scanf("%d",&i);q->front=head;clearone(q,i);break;
case'u':
case'U':q->front=head;clearall(q);break;
case'q':
case'Q':printf("quit!!!\n");
}
if(command=='q'||command=='Q') break;
}
    return 0;
}

# define MAXNUM 70
# define FALSE 0
# define TRUE 1
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct Qnode
{char data[MAXNUM];
struct Qnode *next;
}Qnodetype; /*定义队列的结点*/
typedef struct 
{ Qnodetype *front;/*头指针*/
Qnodetype *rear; /*尾指针*/
int number;/*短信数量*/
}Lqueue;
int initLqueue(Lqueue **q)
{/*创建一个空链队列q*/
*q=(Lqueue *)malloc(sizeof(Lqueue));
if (((*q)->front=(Qnodetype*)malloc(sizeof(Qnodetype)))==NULL) return FALSE;
(*q)->rear=(*q)->front;strcpy((*q)->front->data,"head");
(*q)->front->next=NULL; (*q)->number=0;
return TRUE;}

int LInQueue(Lqueue *q,char x[])
{/*将元素x插入到链队列q中,作为q的新队尾*/
Qnodetype *p;
if ((p=(Qnodetype*)malloc(sizeof(Qnodetype)))==NULL) return FALSE;
strcpy(p->data,x);
p->next=NULL;
q->rear->next=p;/*将链队列中最后一个结点的指针指向新结点*/
q->rear=p;
return TRUE;
}

char * LOutQueue(Lqueue *q)
{/*若链队列q不为空,则删除队头元素,返回其元素值*/
char  x[MAXNUM];
Qnodetype *p;
if(q->front->next==NULL) return NULL;
p=q->front->next;/*取队头*/
q->front->next=p->next;
if (p->next==NULL) q->rear=q->front ;
strcpy(x,p->data);
free(p);
return x;
}

void get(Lqueue *q,char x[])
{ /*接受短信*/
int n;
if (q->number==20)
{LOutQueue(q);q->number--;}
LInQueue(q,x);
q->number++;}

void deleteall(Lqueue *q)
{ /*删除所有短信*/
while (q->front!=q->rear)
  LOutQueue(q);q->number=0;}

void deleteone(Lqueue *q,int n)
{/*删除第n条短信*/
Lqueue *p;Qnodetype *s;
int i;
p=q;i=1;
while (i<n)
{p->front=p->front->next;
i=i+1;}
s=p->front->next;
p->front->next=p->front->next->next;
free(s);
q->number--;}

void displayall(Lqueue *q)
{/*显示所有短信*/
Lqueue *p;char x[MAXNUM];
p=q; 
while(p->front!=q->rear)
{
p->front=p->front->next;
printf("%s\n",p->front->data);
} printf("\n");
}

void displayone(Lqueue *q,int n)
{/*显示第n条短信*/
Lqueue *p;Qnodetype *s;
int i;
p=q;i=1;
while (i<n)
{p->front=p->front->next;
i=i+1;}
s=p->front->next;
printf("%s\n",s->data);
}
void main()
{ Lqueue *Lp; int i;Qnodetype *headnode;
char command,ch[MAXNUM];
initLqueue(&Lp);headnode=Lp->front;

while (1)
{printf("Get information(%d),please enter R\n",Lp->number);
printf("Display one information(%d),please enter L\n",Lp->number);
printf("Display all  information(%d),please enter A\n",Lp->number);
printf("Delete one information(%d),please enter D\n",Lp->number);
printf("Delete all information(%d),please enter U\n",Lp->number);
printf("Quit,please enter Q\n");
printf("please input command:");
scanf("%c",&command);
switch (command)
{case 'r':
case 'R': gets(ch);Lp->front=headnode;get(Lp,ch);break;
case 'l':
case 'L':printf("enter No.:"),scanf("%d",&i);
Lp->front=headnode;displayone(Lp,i);break;
case 'a':
case 'A':Lp->front=headnode;displayall(Lp);break;
case 'd':
case 'D':printf("enter No.:"),scanf("%d",&i);
Lp->front=headnode;deleteone(Lp,i);break;
case 'u':
case 'U':Lp->front=headnode;deleteall(Lp);break;
case 'q':
case 'Q':printf("quit!");
}
if (command=='q'||command=='Q') break;}}

五、实验结果
最后程序运行结果如下所示:
Get information(0),please enter R
Display one information(0),please enter L
Display all information(0),please enter A
Delete one information(0),please enter D
Delete all information(0),please enter U
Quit,please enter Q
please input command: rI like c++↙<回车>
Get information(1),please enter R
Display one information(1),please enter L
Display all information(1),please enter A
Delete one information(1),please enter D
Delete all information(1),please enter U
Quit,please enter Q
please input command

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值