源代码如下:
//链队(尾插法,和头弹出,头删除法;需要设立头节点——频繁的加入、退出 #include#include #define SIZE 10 #define ERROR 0 #define OK 1 #define OVERFLOW 999 #define ElemType int typedef struct Node { ElemType data; struct Node* next; }QueneNode,*QuenePtr; typedef struct { QuenePtr front; QuenePtr tail; }Quenelink; //INIT int QueneINiT(Quenelink *L) { (*L).front = (QueneNode*)malloc(sizeof(QueneNode)); if (!(*L).front) { return ERROR; } (*L).tail = (*L).front; (*L).front->next = NULL; return OK; } //insert int insert(Quenelink*L) { int i = 0; int num = 0; printf("个数>"); scanf("%d", &num); for (i; i < num; i++) { QuenePtr p = (QuenePtr)malloc(sizeof(QueneNode)); printf("输入第%d个值>", i + 1); scanf("%d", &p->data); p->next = (*L).tail->next; (*L).tail->next = p; (*L).tail = p; } return OK; } //clear int clear(Quenelink *L) { QuenePtr p,q; if ((*L).front == (*L).tail) { return ERROR; } p = (*L).front->next; while (p) { q = p->next; if ( q== NULL) { (*L).tail = (*L).front; } free(p); p = q; } (*L).front->next = NULL; (*L).tail = (*L).front; //destory销毁链队 //free((*L).front); } //pop int pop(Quenelink* L) { if ((*L).front == (*L).tail) { return ERROR; } QuenePtr p = (*L).front->next; int e = p->data; QuenePtr q = p->next; (*L).front->next = q; free(p); if (q == NULL) { (*L).tail = (*L).front; } } void traverse(Quenelink L) { QuenePtr p = L.front->next; if (!p) { printf("it's empty"); exit(EXIT_FAILURE); } while (p) { printf("%dt", p->data); p = p->next; } } int main() { Quenelink L; QueneINiT(&L);//一定得传递地址才能够完成初始化,对于链表之类得初始化,都需要传递其头指针的地址完成初始化 insert(&L); delete(&L); traverse(L); return 0; }
今天在写链队的时候,主要是在初始化方面遇到了一些问题:单纯传递链队名称,并不会被初始化:
只有传递地址,才能够成功 :



