数据结构之—C语言达成链式队列

//定义节点typedef struct QNode{QElemType data,include typedef int,typedef struct /,include,typedef int Status,include,C语言队列的一个问题/

数据结构之—C语言完成链式队列

//链式队列的存储
//杨鑫
#include 
#include 
typedef int QElemType;

//定义节点
typedef struct QNode
{
  QElemType data;
  struct QNode *next;
}QNode, *QueuePtr;

//定义指针
typedef struct
{
 QueuePtr front;
 QueuePtr rear;
}LinkQueue;


//插入元素e进入队列
void en_Queue(LinkQueue *q, QElemType e)
{
 QueuePtr temp = (QueuePtr)malloc(sizeof(QNode));
 if(temp)
 {
  temp->data = e;
  temp->next = NULL;
  q->rear->next = temp;
  q->rear = temp;
 }
}


//初始化队列
void init_Queue(LinkQueue *q)
{
  q->front = q->rear = (QNode *)malloc(sizeof(QNode));
  q->front->next = NULL;
}

//创建队列
void create_Queue(LinkQueue *q)
{
  int n = 0;
  init_Queue(q);
  printf("请输入要进入队列的元素,以0结束!\n");
  while(scanf("%d", &n))
  {
   if(n == 0)
     break;
   en_Queue(q, n);
  }
}


//e元素出队
void de_Queue(LinkQueue *q, QElemType *e)
{
 if(q->front == q->rear)
   return;
 QueuePtr temp = q->front->next;
 if(q->front->next == q->rear)
   q->rear = q->front;
 *e = temp->data;
 q->front->next = temp->next;
 free(temp);
}

//判断队列是否为空
int is_Empty(LinkQueue *q)
{
 if(q->front == q->rear)
   return 1;
 return 0;
}

//返回队列
int getlength_Queue(LinkQueue *q)
{
 QueuePtr temp = q->front;
 int i = 0;
 while(temp != q->rear)
 {
  ++i;
  temp = temp->next;
 }
 return i;
}

//清空队列
void clear(LinkQueue *q)
{
 QueuePtr temp = q->front->next;
 while(temp)
 {
  QueuePtr tp = temp;
  temp = temp->next;
  free(tp);
 }
 temp = q->front;
 q->front = q->rear = NULL;
 free(temp);
}

//打印队列元素
void print_Queue(LinkQueue *q)
{
  if(q->front == q->rear)
    return;
  QueuePtr temp = q->front->next;
  while(temp != q->rear)
  {
   printf("%d ", temp->data);
   temp = temp->next;
  }
  printf("%d", temp->data);
  printf("\n");
}


//第一个数据出队
void top_Queue(LinkQueue *q, QElemType *e)
{
 if(q->front == q->rear)
   return;
 *e = q->front->next->data;
}

int main()
{
  int i = 0, k = 0, top = 0;
  int len;
  LinkQueue q;
  create_Queue(&q);
  top_Queue(&q, &top);
  printf("队头的元素为:%d\n", top);
  len = getlength_Queue(&q);
  printf("遍历队中的所有元素:\n");
  for(i = 0; i < len; i++)
  {
   de_Queue(&q, &k);
   printf("%d ", k);
  }
  printf("\n");
  clear(&q);
  return 0;
}

 

 

澳门京葡网站 1

//链式队列的存储//杨鑫#include #include typedef int
QElemType;//定义节点typedef struct QNode{QElemType data;struct QNode
*n…

#include <iostream>
#include<string.h>static int n; //用于输入货架的大小using
namespace std;
//新闻结构体
typedef struct /*Inform*/
//能够去掉Inform,在要求在结构体中定义结构体对象时不可能去掉
{
string name;
int a;
}Inform;//栈的*顺序*结构体
typedef struct Node
{
Inform e;
struct Node *next;
}StackNode,*LinkStack;//队列*链式*结构体
typedef struct QNode
{
Inform e;
struct QNode *next;
}QNode,*QueuePtr;//指向链式队列的头尾指针
typedef struct
{
QueuePtr fro;
QueuePtr rear;
}LinkQueue;void Found1(LinkStack &s); //货色上架
void Foundd(LinkQueue &d); //创制顺序队列
void TurnsZ1(LinkStack &s1,LinkQueue d); //对于后天未剩余倒货
void TurnsZ2(LinkStack &s1,LinkStack s2,LinkStack s3);
//对于明天有多余倒货
void Print(LinkStack s1);int main()
{
cout << “请输入每一趟上货的最大值\t”;
cin >> n;
LinkStack s1,s2,s3;
s1=s2=s3=NULL; //是还是不是有不可缺少定义构造函数,即创建三个空栈
营造二个队列,使其头尾指针指向队列未存货品音讯的空结点
LinkQueue d;
cout<<“第一天货色上架”<<endl;
Found1; //输入物品消息
Foundd;
TurnsZ1;
Print;
cout<<“第二天货品上架”<<endl;
if
{
Found1;
TurnsZ1;
Print;
}
else
澳门京葡网站,{
Found1;
TurnsZ2;
Print;
}
return 0;
}void Found1(LinkStack &s)
{
s=NULL; //定义二个空栈,s连接在p上,即p->next=NULL
Inform i;
i.name=””;
cout<<“输入break代表甘休输入:”<<endl;
while
{
StackNode *p;
p=new StackNode;
cin>>i.name;
if(i.name==”break”)
break;
cin>>i.a;
p->e=i;
p->next=s;
s=p;
}
}void Foundd(LinkQueue &q)
{
q.fro=q.rear=new QNode;
q.fro->next=NULL;
q.rear->next=NULL;
}void TurnsZ1(LinkStack &s1,LinkQueue d) //此处队列表现出来的主题素材为
d.fro始终与d.rear相等。实际上为对接点的创办(多次施用只创建一次导致直接在修该此指针

c语言这几个队列写的反常呢,为啥出队的因素都为0,是入队写错了,还是出队的错了?
#include
#include
typedef int QElemType;
typedef int Status;
typedef struct QNode {
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=malloc(sizeof;
if(!Q->front)exit;
Q->front=NULL;
return 1;
}
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr p;
p=malloc(sizeof;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
Status Dequeue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)Q->rear=Q->front;
free;
return 1;
}
Status DestroyQueue(LinkQueue *Q)
{
while(Q->front){
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
return 1;
}
void main()
{
int i,e;
LinkQueue Q;
InitQueue;
for(i=0;i<10;i++)
{
EnQueue;
}
for(i=0;i<8;i++)
{
Dequeue(&Q,&e);
printf;
}
DestroyQueue;
system;
}

C语言队列的贰个标题/ 100C
#include “stdafx.h”
#include
#include
#include
#include
#define MAX_SIZE 20
#define ADD_SIZE 2
#define LENGTH 15
#define FALSE -1
#define TRUE 1
typedef char QElemType;
typedef int Status;
typedef struct QNode {
QElemType data[LENGTH];
struct QNode *next;
}QNode, *QueuePtr;

//而未存款和储蓄下三个结点)还也许有delete的标题,怎填的结点指针并不能delete,不然该节点将会被删去
{
Inform i;
//QNode *p=new QNode;
如果未有上边包车型客车,知识在修改队列第二个要素指向的值,并非接二连三累加

StackNode *s=new StackNode;
while
{
QNode *p=new QNode;
//此处十一分器重

i=s1->e; //i=s1.e不科学
,调用对象的积极分鸡时用“.”,假若是指针只怕援引时,利用“->”调用其成员
s=s1;
s1=s->next;
delete s;
p->e=i;
p->next=NULL;
d.rear->next=p;
d.rear=p;
//cout<<“rear指向的名字”<<d.rear->e.name<<endl;
//cout<<“fro指向的名字”<<d.fro->next->e.name<<endl;
//《难点》此处fro 与 rear 同期活动
}
QNode *p=new QNode;
while(d.fro!=d.rear)
{
StackNode *s=new StackNode;
p=d.fro->next;
i=p->e;
d.fro->next=p->next;
if(p==d.rear)
d.rear=d.fro;
delete p;
s->e=i;
s->next=s1;
s1=s;
}
}void TurnsZ2(LinkStack &s1,LinkStack s2,LinkStack s3)
{
Inform i;
while
{
StackNode *s=new StackNode;
s=s1;
s1=s->next;
s->next=s3;
s3=s;
}
while
{
StackNode *s=new StackNode;
s=s2;
s2=s->next;
s->next=s1;
s1=s;
}
while
{
StackNode *s=new StackNode;
s=s3;
s3=s->next;
s->next=s1;
s1=s;
}
}void Print(LinkStack s1)
{
//StackNode *s=new StackNode;
while
{
cout<<s1->e.name<<” “<<s1->e.a<<endl;
s1=s1->next;
}
}栈与队列链式存款和储蓄结构:
1:掌握栈与队列的早先化难题,能够透过画画进一步精晓并回忆其大旨内容 a:栈,初阶化时申请叁个 *S
指向NULL的空地址,存入时,申请空间并与S连接就可以(注意栈的尾巴结点的next->NULL)
b: 队列:首先成立多个指针 *pro he *rear
指向同三个空节点,并令其next->NULL,存入时,创立新结点连在rear前边,况且将rear指向新节点,删除队列成分时,pro不
动,删除pro->next就能够

2:极其问题:
在存入和删除成分时,要注意新节点的创立与删除。
a:创立——存入一个要素须要创制叁个新指针,不可能创立贰个往往对其赋值,那样不会存入,而是在不停改动第叁个要素的值
b:delete也是这么,在要素存入时,若delete存入的指针,则会使存入的因素空间释放,即存入进去的剧情被删去

typedef struct {
QueuePtr front;
QueuePtr rear;
int number;
}Message;
Status CreatMessageQueue(Message **m) {
if ->front = malloc(sizeof)) == NULL)
return FALSE;
->rear = ->front; //开头时为空队列
strcpy->front->data, “head”); //设立三个头成分
->front->next = NULL;
->number = 0; //标志设0

return TRUE;

}