具体代码:
//队列就是用Front和TailC操作节点形成队列(与栈相比就是变成了尾插入,获取都是从头获取元素) #include<stdio.h> #include<stdlib.h> //节点定义 struct Node { int data; struct Node* next; }; //节点创建 struct Node* createNode(int data) { struct Node* newNode=(struct Node*)malloc(sizeof(struct Node)); newNode->data=data; newNode->next=NULL; return newNode; } //队列定义 struct Queue { struct Node* frontNode; struct Node* tailNode; int size; }; //队列创建(需要建立一个队列的变量,再返回一个指针,用这个指针操作) struct Queue* createQueue() { struct Queue* myQueue=(struct Queue*)malloc(sizeof(struct Queue)); myQueue->frontNode=myQueue->tailNode=NULL; myQueue->size=0; return myQueue; } //插入元素 void push(struct Queue* myQueue,int data) { struct Node* newNode=createNode(data); if(myQueue->size==0) { myQueue->frontNode=myQueue->tailNode=newNode; } else { myQueue->tailNode->next=newNode; myQueue->tailNode=newNode; } myQueue->size++; } //删除元素 void pop(struct Queue* myQueue) { if(myQueue->size==0) { printf("The Queue is empty! "); } else { struct Node* deNode=myQueue->frontNode; myQueue->frontNode=myQueue->frontNode->next; free(deNode); } myQueue->size--; } //获取队列头元素 int front(struct Queue* myQueue) { if(myQueue->size==0) { printf("The Queue is empty! "); } else { return myQueue->frontNode->data; } } //判断队列是否为空 int isEmpty(struct Queue* myQueue) { if(myQueue->size==0) { return 0; } else { return 1; } } int main() { struct Queue* myQueue=createQueue(); push(myQueue,1); push(myQueue,2); push(myQueue,3); while(isEmpty(myQueue)) { printf("%d ",front(myQueue)); pop(myQueue); } printf(" "); return 0; }
效果展示:
自己练习的:
#include<stdio.h> #include<stdlib.h> typedef struct Node { char data; struct Node* next; }NODE,*LPNODE; typedef struct stack { int size; LPNODE stackTop; }STACK,*LPSTACK; LPNODE createNode(char data) { LPNODE newNode=(LPNODE)malloc(sizeof(NODE)); newNode->data=data; newNode->next=NULL; return newNode; } LPSTACK createStack() { LPSTACK st=(LPSTACK)malloc(sizeof(STACK)); st->size=0; st->stackTop=NULL; return st; } void insertNode(LPSTACK st,char data) { LPNODE newNode=createNode(data); if(st->size==0) { st->stackTop=newNode; st->size++; } else { newNode->next=st->stackTop;//最重要的一步,这里并不是newNode->next=st->stackTop->next;而是直接连接上stackTop指向的,再移动指针 st->stackTop=newNode; st->size++; } } void printStack(LPSTACK st) { LPNODE pmove=st->stackTop; while(pmove!=NULL) { printf("%c ",pmove->data); pmove=pmove->next; } } int main() { LPSTACK st=createStack(); insertNode(st,'A'); insertNode(st,'B'); insertNode(st,'C'); printStack(st); return 0; }
自己练习的(结果为 A B C)
#include<stdio.h> #include<stdlib.h> typedef struct Node { char data; struct Node* next; }NODE,*LPNODE; typedef struct Queue { int size; LPNODE frontNode; LPNODE rearNode; }QUEUE,*LPQUEUE; LPNODE createNode(char data) { LPNODE newNode=(LPNODE)malloc(sizeof(NODE)); newNode->data=data; newNode->next=NULL; return newNode; } LPQUEUE createQueue() { LPQUEUE qu=(LPQUEUE)malloc(sizeof(QUEUE)); qu->frontNode=NULL; qu->rearNode=NULL; qu->size=0; return qu; } void insertNode(LPQUEUE qu,char data) { LPNODE newNode=createNode(data); if(qu->size==0) { qu->frontNode=qu->rearNode=newNode; qu->size++; } else { qu->rearNode->next=newNode; qu->rearNode=newNode; qu->size++; } } void printQueue(LPQUEUE qu) { LPNODE pmove=qu->frontNode; while(pmove!=NULL) { printf("%c ",pmove->data); pmove=pmove->next; } } int main() { LPQUEUE qu=createQueue(); insertNode(qu,'A'); insertNode(qu,'B'); insertNode(qu,'C'); printQueue(qu); return 0; }