c语言链队基本函数 c语言链表函数

谁做过c语言的 队列及其操作啊我自己写了一个,练练手
由于没有指定数据类型,在第三行处我用float类型了
如果改成其它类型,代码中有//Type处,涉及输入输出,也要相应的改一下
【c语言链队基本函数 c语言链表函数】这个代码只做了简单的测试,不保证正确性
#includestdio.h
#includemalloc.h
#define Type float
struct node{
Type data;
struct node *next;
};
struct queue{
struct node *front,*rear;
};
struct node * push(Type t,struct queue *q)
{
struct node *last;
last=(struct node *)malloc(sizeof(struct node));
last-data=https://www.04ip.com/post/t;
last-next=NULL;
if(q-rear!=NULL){
q-rear-next=last;
q-rear=last;
}else{
q-front=last;
q-rear=last;
}
return last;
}
Type pop(struct queue *q)
{
struct node nd;
if(q-front==NULL){
printf("Error:queue is blank\n");
return 0;
}
nd=*(q-front);
free(q-front);
if(q-front!=q-rear)
q-front=nd.next;
else{
q-front=NULL;
q-rear=NULL;
}
return nd.data;
}
void clear(struct queue *q)
{
struct node *np,*np1;
if(q-front!=NULL){
np=q-front;
while(1){
np1=np;
free(np);
if(np==q-rear)
break;
np=np1-next;
}
q-front=NULL;
q-rear=NULL;
}
printf("The queue has been cleared\n");
}
struct queue * init()
{
struct queue *q=(struct queue *)malloc(sizeof(struct queue));
q-front=NULL;
q-rear=NULL;
return q;
}
void display(struct queue *q)
{
struct node *np;
np=q-front;
if(np==NULL){
printf("The queue is blank\n");
return;
}
while(1){
printf("%f\n",np-data);//Type
if(np==q-rear)
break;
np=np-next;
}
}
int main()
{
struct queue *q;
unsigned int c;
Type t;
q=init();
printf("One blank queue has been created\n");
while(1){
printf("Menu:\
\n*******************************************************\
\n\t1:push an element to queue\
\n\t2:pop an element from queue\
\n\t3:clear queue\
\n\t4:display queue\
\n\t5:exit\
\n*******************************************************\
\nPlease select operator\n");
scanf("%d",c);
switch(c){
case 1:
printf("Please input the element pushed to queue:\n");
scanf("%f",t);//Type
push(t,q);
break;
case 2:
printf("The element poped from queue is %f\n",pop(q));//Type
break;
case 3:
clear(q);
break;
case 4:
display(q);
break;
case 5:
clear(q);
free(q);
break;
default:
printf("Invalid input\n");
break;
}
if(c==5)
break;
}
return 0;
}
用C语言写一个链队,但是段错误 , 求解答,在线等,代码如下://在push函数里面 , 加入节点后指针需要后移;我试了一下 , 可以正常运行
#include stdio.h
#include stdlib.h
typedef struct Node
{
int m_data;
struct Node* m_next;
}NODE;
typedef struct Queue
{
NODE* m_head;
NODE* m_rear;
}QUEUE;
void InitialQueue(QUEUE* queue)
{
queue - m_head = queue - m_rear = NULL;
}
void push(QUEUE* queue, int data)
{
NODE* node = (NODE *)malloc(sizeof(NODE));
node - m_data = https://www.04ip.com/post/data;
node - m_next = NULL;
if (! queue - m_rear)
{
queue - m_head = queue - m_rear = node;
}
else
{
queue - m_rear - m_next = node;
//加入节点后指针后移
queue-m_rear = queue-m_rear-m_next;
}
}
int pop(QUEUE* queue)
{
if (! queue - m_rear)
{
printf("队列已空!\n");
return -1;
}
else if (! queue - m_rearqueue - m_head == queue - m_rear)
{
int data = https://www.04ip.com/post/queue - m_rear - m_data;
free(queue - m_rear);
queue - m_head = queue - m_rear = NULL;
return data;
}
else
{
NODE* node = queue - m_head - m_next;
int data = https://www.04ip.com/post/queue - m_head - m_data;
free(queue - m_head);
queue - m_head = node;
return data;
}
}
int main(void)
{
QUEUE queue;
InitialQueue(queue);
push(queue, 1);
push(queue, 2);
push(queue, 3);
printf("%d\n", pop(queue));
printf("%d\n", pop(queue));
printf("%d\n", pop(queue));
return 0;
}
数据结构(c语言版)队列基本操作的实现/***************/
/* 链式队列*/
/***************/
#include "stdlib.h"
#include "stdio.h"
/* 定义链式队列类型 */
typedef int ElemType;
typedef struct QNode
{ ElemTypedata;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct
{ QueuePtr front;
QueuePtr rear;
} LinkQueue;
/* 1、初始化链式队列 */
void InitQueue(LinkQueue *Q)
{ Q-front=Q-rear=(QueuePtr)malloc(sizeof(QNode));
if (!(Q-front)) exit(0);
Q-front-next=NULL; }
/* 2、销毁链式队列 */
void DestroyQueue(LinkQueue *Q)
{ while (Q-front)
{ Q-rear=Q-front-next;
free(Q-front);
Q-front=Q-rear; }
}
/* 3、清空链式队列 */
void ClearQueue(LinkQueue *Q)
{ QueuePtr p;
p=Q-front-next;
while (p)
{ Q-front-next=p-next;
free(p); }
Q-rear=Q-front;
}
/* 4、判断空队列 */
int QueueEmpty(LinkQueue Q)
{ if (Q.front==Q.rear)
return 1;
else
return 0; }
/* 5、求链式队列长度 */
int QueueLength(LinkQueue Q)
{ QueuePtr p; int n=0;
p=Q.front;
while (p!=Q.rear)
{ n; p=p-next; }
return n;
}
/* 6、取队头元素 */
ElemType GetHead(LinkQueue Q)
{ if (Q.front!=Q.rear)
return Q.front-next-data;
}
/* 7、入队列 */
void EnQueue(LinkQueue *Q, ElemType e)
{ QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if (!p) exit(0);
p-data=https://www.04ip.com/post/e; p-next=NULL;
Q-rear-next=p;
Q-rear=p; }
/* 8、出队列 */
void DeQueue(LinkQueue *Q, ElemType *e)
{ QueuePtr p;
if (Q-front!=Q-rear)
{ p=Q-front-next;
*e=p-data;
Q-front-next=p-next;
if (Q-rear==p) Q-rear=Q-front;
free(p); }
}
/* 9、遍历链式队列并输出元素 */
void QueueTraverse(LinkQueue Q)
{ QueuePtr p;
printf("\nQueue: ");
p=Q.front-next;
while (p)
{ printf("%d\t",p-data);
p=p-next;}
}
/* 约瑟夫问题 */
void Joseffer(int n)
{ LinkQueue Q; int i; ElemType x;
InitQueue(Q);
for(i=1; i=n; i)
EnQueue(Q,i);
while (!QueueEmpty(Q))
{ for(i=1; i=3; i)
{ DeQueue(Q,x);
if (i!=3)
EnQueue(Q,x);
else
printf("]",x);
}
}
}
/* 主函数 */
main()
{ LinkQueue Q; int i; ElemType x;
InitQueue(Q);
for(i=2; i=5; i)
EnQueue(Q,i);
printf("len:%d\n",QueueLength(Q));
while (!QueueEmpty(Q))
{ DeQueue(Q,x);
printf("%d\t",x); }
//QueueTraverse(Q);
//Joseffer(6);
}
自己去调试吧c语言链队基本函数,这个是C语言版的链式队列c语言链队基本函数,如果看不懂或者调不出来就去看书吧 。否则c语言链队基本函数你这门是白学了 。
注c语言链队基本函数:这里是链式队列
/***************/
/* 循环队列*/
/***************/
#include "stdlib.h"
#include "stdio.h"
#define N 100
/* 定义循环队列类型 */
typedef int ElemType;
typedef struct
{ ElemType*base;
int front;
int rear;
} SqQueue;
/* 1、初始化循环队列 */
void InitQueue(SqQueue *Q)
{ Q-base=(ElemType*)malloc(N*sizeof(ElemType));
Q-front=Q-rear=0; }
/* 2、销毁循环队列 */
void DestroyQueue(SqQueue *Q)
{ free(Q-base); }
/* 3、清空循环队列 */
void ClearQueue(SqQueue *Q)
{ Q-front=Q-rear=0; }
/* 4、判断空队列 */
int QueueEmpty(SqQueue Q)
{ if (Q.front==Q.rear)
return 1;
else
return 0; }
/* 5、求循环队列长度 */
int QueueLength(SqQueue Q)
{ return (Q.rear N-Q.front)%N; }
/* 6、取队头元素 */
void GetHead(SqQueue Q, ElemType *e)
{ if (Q.front!=Q.rear)
*e=Q.base[Q.front];
}
/* 7、入队列 */
int EnQueue(SqQueue *Q, ElemType e)
{ if ((Q-rear 1)%N==Q-front)
return 0;
Q-base[Q-rear]=e;
Q-rear=(Q-rear 1)%N;
return 1; }
/* 8、出队列 */
int DeQueue(SqQueue *Q, ElemType *e)
{ if (Q-front==Q-rear)
return 0;
*e=Q-base[Q-front];
Q-front=(Q-front 1)%N;
return 1; }
/* 9、遍历循环队列并输出元素 */
void QueueTraverse(SqQueue Q)
{ int i;
printf("\nQueue: ");
if (Q.rearQ.front) Q.rear=Q.rear N;
for(i=Q.front; iQ.rear; i)
printf("%d\t",Q.base[i%N]); }
/* 主函数 */
main()
{ SqQueue Q; int i; ElemType x;
InitQueue(Q);
for(i=2; i=5; i)
EnQueue(Q,i);
printf("len:%d\n",QueueLength(Q));
while (!QueueEmpty(Q))
{ DeQueue(Q,x);
printf("%d\t",x); }
QueueTraverse(Q);
}
在给c语言链队基本函数你个循环队列吧
数据结构C语言描述的链队列的基本操作(初始化,判空,入队 , 出队,取对头,输出队列所有值....)void InitQueue(LiQueue *q)
{q=(LiQueue *)malloc(sizeof(LiQueue));
q-front=q-rear-NULL;}//初始化
int QueueEmpty(LiQueue *q)
{if(q-rear==NULL)
return 1;
else
return 0;}//判空
void enQueue( LiQueue *q,ElemType e)
{QNode *s;
s=(QNode*)malloc(sizeof(QNode));
s-data=https://www.04ip.com/post/e;
s-next=NULL;
if(q-rear==NULL)
q-front=q-rear=s;
else
{q-rear-next=s;
q-rear=s;
}}//入队
intdeQueue( LiQueue *q,ElemType e)
{QNode *t;
if(q-rear==NULL)
return 0;
t=q-front;
if(q-front==q-rear)
q-front=q-rear=NULL;
else
q-front=q-front-next;
e=t-data;
free(t);
return 1;}//出队
int deQueue( LiQueue *q,ElemType e)
{QNode *t;
if(q-rear==NULL)
return 0;
t=q-front;
if(q-front==q-rear)
q-front=q-rear=NULL;
else
q-front=q-front-next;
e=t-data;break;
free(t);
return 1;}//取队头
输出队列所有数就是出队
C语言数据结构链队的主函数怎么调用?主函数怎么写?主函数里调用就像写函数定义一样,比如调用创建表的,就这样:
#include stdio.h
struct Linklist {
...
};
typedef Linklist* LinkList;
int CreateList(LinkList LstMe) {
...
}
int main() {
LinkList LstDemo = (LinkList) malloc (sizeof(Linklist));
CreateList(LstDemo); // 调用建表
free (LstDemo);
return 0;
}
c语言 队列的操作//定义队列结构体
typedef struct Qnode
{
int data;
struct Qnode *next;
} Queue , *QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
} linkQnode;
//创建一个队列
initQueue (linkQnode *q)
{
q - front = q - rear = (QueuePtr) malloc (sizeof (Queue));
if (!q - front) exit (0);
q - front - next = NULL;
}
//入队列
EnterQueue (linkQnode *q , int item)
{
QueuePtr p;
p = (QueuePtr) malloc (sizeof (Queue));
if (!p) exit (0);
p - data = https://www.04ip.com/post/item;
p - next = NULL;
q - rear - next = p;
q - rear = p;
}
//出队列
DelQueue (linkQnode *q , int *item)
{
QueuePtr p;
if (q - front = q - rear) return;
p = q - front - next;
*item = p - data;
q - front - next = p - next;
if (q - rear == p)
q - rear = q - front;
free (p);
}
c语言链队基本函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言链表函数、c语言链队基本函数的信息别忘了在本站进行查找喔 。

    推荐阅读