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;
}
#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;
}
阅读全文
0 0
- C语言数据结构单链队列的操作集
- C语言 单链队列操作
- C语言 单链队列操作
- C语言数据结构循环队列的操作集
- 数据结构之顺序队列的操作(C语言)
- 数据结构之顺序队列的操作(C语言)
- 算法与数据结构-队列的基本操作C语言实现
- 数据结构循环队列的基本操作(C语言)
- 单链队列 - C语言
- C语言实现单链队列的基本操作(五)
- C语言数据结构-链队列
- 数据结构-单链队列操作算法
- C语言 数据结构 队列 链队列
- C语言链队列的基本操作
- C语言数据结构栈的操作集
- C语言数据结构双向链表的操作集
- 数据结构之队列的实现(c语言)
- [数据结构]C语言队列的实现
- 51nod 1204 Parity(并查集)
- Hbase架构与原理
- Hanoi塔问题
- 线性表
- RIPv2协议
- C语言数据结构单链队列的操作集
- jq的 $(function(){}) , document.ready 与 window.onload 的区别
- 利用python进行微信好友分析
- stored-program Computer/von Neumann Architecture 冯诺依曼体系结构
- python学习中遇到的问题整理
- Java文件创建删除复制读写以及查找盘符文件操作
- CString 变 char
- 内容提供者-Content Provider
- SpringCloud第七篇-Spring Cloud Config