链队列的相关操作:
#include#include #include #define MAX_SIZE 100 #define OVERFLOW 1 typedef struct LinkNode_{ int data; LinkNode_ *next; }LinkNode,*linknode; typedef struct LinkQueue_{ linknode front; linknode rear; }LinkQueue ,*linkqueue; linkqueue initqueue(){ linkqueue resultptr=(linkqueue)malloc(sizeof(LinkQueue)); linknode headnode=(linknode)malloc(sizeof(LinkNode)); headnode->next=NULL; headnode->data=-1; resultptr->front=headnode; resultptr->rear=headnode; return resultptr; } void printqueue(linkqueue l){ if(l->front==l->rear){ printf("队列为空!n"); exit(OVERFLOW); } for(linknode p=l->front->next;p;p=p->next) { printf("%d ",p->data); } printf("rn"); } linkqueue addnode(linkqueue l,int var){ linknode p=(linknode)malloc(sizeof(LinkNode)); p->data=var; p->next=NULL; l->rear->next=p; l->rear=p; return l; } linkqueue outputnode(linkqueue l){ if (!l->front->next){ printf("队列为空!n"); exit(OVERFLOW); } linknode q=l->front->next; l->front->next=l->front->next->next; q=NULL; free(q); return l; } int outputdata(linkqueue l){ if (!l->front->next){ printf("队列为空!n"); exit(OVERFLOW); } return l->front->next->data; } void testsample(){ linkqueue l=initqueue(); l=addnode(l,1); l=addnode(l,2); l=addnode(l,3); l=addnode(l,4); printqueue(l); int rear=outputdata(l); printf("出队:%dn",rear); l=outputnode(l); printf("再次打印:") ; printqueue(l); } int main(){ testsample(); return 0; }
运行结果:
总结:
写队列的相关操作时要注意与栈区分(先进先出),其他好像就没啥可总结的了



