#include<stdio.h>
#include <stdlib.h>
typedef struct Linknode{
int data;
struct Linknode *next;
}Linknode;
typedef struct{
Linknode *front;
Linknode *rear;
}Linksqueue;
void Initsqueue(Linksqueue &s){ //初始化队列
s.front==NULL;
s.rear==NULL;
}
bool insert(Linksqueue &s,int x){ //插入元素
Linknode *p=(Linknode*)malloc(sizeof(Linknode));
p->data=x;
p->next=NULL;
if(s.front==NULL)
{
s.front=p;
s.rear=p;
}
else{
s.rear->next=p;
s.rear=p;
}
printf("插入成功\n");
return true;
}
void look(Linksqueue &s){ //查看队列元素
if(s.front==NULL){
printf("队列为空\n");
}
Linknode *p;
p=s.front;
int i=1;
while(p!=NULL){
printf("第%d个元素为%d\n",i,p->data);
i++;
p=p->next;
}
}
bool Delete (Linksqueue &s,int &x){ //删除元素,出队
if(s.front==NULL){
printf("队列为空\n");
return false;
}
Linknode *p=s.front;
x=p->data;
s.front=p->next;
printf("删除的元素为%d\n",x);
if(s.rear=p){
s.rear=NULL;
}
free(p);
return true;
}
int main(){
Linksqueue s;
Initsqueue(s);
insert (s,1);
insert (s,2);
insert (s,3);
insert (s,4);
look(s);
int x;
Delete(s,x) ;
look(s);
return 0;
}
(数据结构)无头结点的队列链式存储结构
最新推荐文章于 2024-07-24 22:13:06 发布