C语言数据结构单链队列的操作集

来源:互联网 发布:ubuntu下apache 2配置 编辑:程序博客网 时间:2024/05/18 01:14
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode,*queueptr;
typedef struct{
   queueptr front;
   queueptr rear;
}linkqueue;


void createqueue(linkqueue &Q,ElemType n)
{
queueptr q;
int i;
Q.front=Q.rear=(queueptr)malloc(sizeof(QNode));
if(!Q.front) printf("分配空间失败!大兄弟!\n");
Q.rear->next=NULL;
    for(i=0;i<n;i++)
{
q=(queueptr)malloc(sizeof(QNode));
scanf("%d",&q->data);
q->next=NULL;
Q.rear->next=q;
Q.rear=q;
}
}


void insertqueue(linkqueue &Q,ElemType e)
{
queueptr p;
p=(queueptr)malloc(sizeof(QNode));
if(!p)printf("分配储存空间失败!\n");
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}


void deletequeue(linkqueue &Q)
{
queueptr p;
int e;
if(Q.front==Q.rear)
printf("尼玛啊!都没了还让我删除!\n");
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
printf("删除的元素是%d\n",e);
}


void destroyqueue(linkqueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}


}






void print(linkqueue &Q)
{
queueptr q;
q=Q.front->next;
printf("遍历这个队列存的是啥:\n");
while(q)
{
printf("%3d",q->data);
q=q->next;
}


}


int main()
{
linkqueue q;
int n,choice;
int e;
printf("输入1是创建单链队列并打印\n输入2是插入元素并打印\n输入3是出队一个元素\n输入4是销毁队列\n************正在等待用户输入**********\n");
while(scanf("%d",&choice)!=EOF)
{
if(choice==1)
{
printf("创建队列并输入数据的个数:\n");
scanf("%d",&n);
createqueue(q,n);
print(q);
}
if(choice==2)
{
printf("请输入要入队的元素:\n");
scanf("%d",&e);
insertqueue(q,e);
print(q);
}
        if(choice==3)
{
deletequeue(q);
print(q);
}
if(choice==4)
{
destroyqueue(q);
printf("大兄弟,已经帮你解决了那个队列!\n");
}


printf("\n********能不能快点输入,急死了!!!****\n");
}




return 0;
}











原创粉丝点击