本文的队列没有建立尾节点而是每次遍历到最后一个节点,在队列的相关操作中不太规范。
1.栈的增删与测试
#include <malloc.h>
#include <stdio.h>
#define Maxsize 10
/*顺序栈的数据结构定义*/
typedef struct {
int data[Maxsize];
int top;
}Stack;
/*链表栈的数据结构*/
typedef struct Stacknode {
int data;
Stacknode* next;
}Stacklist, Stacknode;
/*初始化栈*/
void Initial(Stack &S) {
S.top = -1;
}
/*栈判空*/
int StackEmpty(Stack S) {
if (S.top == -1) {
return 1;
}
return 0;
}
/*栈判满*/
int StackFull(Stack S) {
if (S.top == Maxsize - 1) {
return 1;
}
return 0;
}
/*栈的push操作*/
int push(Stack& S, int val) {
if (StackFull(S)) {
return 1;
}
S.data[++S.top] = val;
return 0;
}
/*栈的pop操作*/
int pop(Stack &S, int &val) {
if (StackEmpty(S)) {
return 1;
}
val = S.data[S.top--];
return 0;
}
Stacklist* initStacklist(Stacklist* head) {
head->next = NULL;
head->data = 0;
return head;
}
int listStackempty(Stacklist* head) {
if (head->data == 0) {
return 1;
}
return 0;
}
int listStackfull(Stacklist* head) {
if (head->data == Maxsize) {
return 1;
}
return 0;
}
Stacklist* listpush(Stacklist* head, int val) {
Stacknode* node;
node = (Stacknode*)malloc(sizeof(Stacknode));
if (node == NULL || listStackfull(head)) {
return NULL;
}
node->next = head->next;
head->next = node;
node->data = val;
head->data++;
return head;
}
Stacklist* listpop(Stacklist* head) {
if (listStackempty(head)) {
return NULL;
}
head->data--;
head->next = head->next->next;
return head;
}
int main() {
Stacklist* S;
int val;
int count = Maxsize;
Stacklist* rc;
S = (Stacklist*)malloc(sizeof(Stacklist));
if (S == NULL) {
return 1;
}
S = initStacklist(S);
while(count) {
printf("plead enter the num to push:\n");
scanf_s("%d", &val);
S = listpush(S, val);
count--;
}
/*链表栈的测试*/
printf("\n");
//while (count != 5) {
// S = listpop(S);
// count++;
//}
while (S->next) {
printf("%d\n", S->next->data);
S = S->next;
}
return 0;
/*
* 顺序栈的测试
int rc;
int value = 10;
int rec;
int count = 10;
Stack S;
Initial(S); //初始化栈
while (count) {
scanf_s("%d", &rec);
push(S, rec);
count--;
}
//push(S, value);
count = 10;
while (count) {
pop(S, rec);
count--;
}
//pop(S, rec);
rc = StackEmpty(S);
if (rc) {
printf("Stack is empty\n");
}
rc = StackFull(S);
if (rc) {
printf("Stack is full\n");
}
printf("%d",rec);
return 0;
*/
}
2.顺序表实现队列增删
#include <stdio.h>
#define Maxsize 10
//顺序表实现队列
typedef struct Queue {
int data[Maxsize];
int size;
}Queue;
//初始化队列
int initQueue(Queue& Q) {
int i;
for (i = 0; i < Maxsize; i++) {
Q.data[i] = 0;
}
Q.size = 0;
return 1;
}
//头部入队
int headEnqueue(Queue& Q, int num) {
int i;
if (Q.size == 0) {
Q.data[0] = num;
}
else if(Q.size == Maxsize){
printf("\nQueue is full!\n");
return 0;
}
else {
for (i = Q.size - 1; i >= 0; i--) {
Q.data[i + 1] = Q.data[i];
}
Q.data[0] = num;
}
Q.size++;
return 1;
}
//头部出队
int headDequeue(Queue& Q) {
int i;
if (Q.size == 0) {
return 0;
}
else{
for (i = 0; i < Q.size - 1; i++) {
Q.data[i] = Q.data[i + 1];
}
Q.data[Q.size - 1] = 0;
}
Q.size--;
return 1;
}
//尾部入队
int tailEnqueue(Queue& Q, int num) {
if (Q.size == Maxsize) {
printf("\nQueue is full!\n");
return 0;
}
Q.data[Q.size] = num;
Q.size++;
return 1;
}
//尾部出队
int tailDequeue(Queue& Q) {
if (Q.size == 0) {
return 0;
}
Q.size--;
return 1;
}
int main() {
Queue Q;
int num, seq;
int i;
initQueue(Q);
while (true) {
printf("1.head enqueue\n2.head dequeue\n3.tail enqueue\n4.tail dequeue\n");
printf("Please select your operation:");
scanf_s("%d", &seq);
switch (seq){
case(1):{
printf("please enter the num to headEnqueue: ");
scanf_s("%d", &num);
headEnqueue(Q, num);
break;
}
case(2):{
headDequeue(Q);
break;
}
case(3): {
printf("please enter the num to tailEnqueue: ");
scanf_s("%d", &num);
tailEnqueue(Q, num);
break;
}
case(4): {
tailDequeue(Q);
break;
}
default:{
printf("\nplease enter the num between 1 and 4");
break;
}
}
printf("\nThe present data int queue are:");
for (i = 0; i <= Q.size-1; i++) {
printf("%d ", Q.data[i]);
}
printf("\n");
}
return 0;
}
3.带头节点链表实现队列增删
#include <stdio.h>
#include <malloc.h>
typedef struct Queue {
int value;
Queue* next;
}Queue,Qnode;
Queue *initQueue(Queue* Q) {
Q->value = 0;
Q->next = NULL;
return Q;
}
Queue* headDequeue(Queue* Q) {
if (Q->value == 0) {
return NULL;
}
Q->next = Q->next->next;
Q->value--;
return Q;
}
Queue* tailDequeue(Queue* Q) {
if (Q->value == 0) {
return NULL;
}
Qnode* current;
current = Q;
while (current->next->next) {
current = current->next;
}
free(current->next);
current->next = NULL;
Q->value--;
return Q;
}
Queue* headEnqueue(Queue* Q,int num) {
Qnode* newnode;
newnode = (Queue*)malloc(sizeof(Qnode));
if (newnode == NULL) {
return NULL;
}
newnode->value = num;
newnode->next = NULL;
newnode->next = Q->next;
Q->next = newnode;
Q->value++;
return Q;
}
Queue* tailEnqueue(Queue* Q, int num) {
Qnode* current;
Qnode* newnode;
newnode = (Qnode*)malloc(sizeof(Qnode));
if (newnode == NULL) {
return NULL;
}
current = Q;
while (current->next) {
current = current->next;
}
newnode->value = num;
newnode->next = NULL;
current->next = newnode;
Q->value++;
return Q;
}
int main() {
Queue *Q;
int seq, num;
Q = (Queue*)malloc(sizeof(Queue));
if (Q == NULL) {
return 0;
}
Q = initQueue(Q);
Qnode* current;
current = (Qnode*)malloc(sizeof(Qnode));
if (current == NULL) {
return 0;
}
current = initQueue(current);
while (true) {
printf("1.head enqueue\n2.head dequeue\n3.tail enqueue\n4.tail dequeue\n");
printf("Please select your operation:");
scanf_s("%d", &seq);
switch (seq){
case(1):{
printf("please enter the num to headEnqueue: ");
scanf_s("%d", &num);
Q = headEnqueue(Q, num);
break;
}
case(2):{
Q = headDequeue(Q);
break;
}
case(3): {
printf("please enter the num to tailEnqueue: ");
scanf_s("%d", &num);
Q = tailEnqueue(Q, num);
break;
}
case(4): {
Q = tailDequeue(Q);
break;
}
default:{
printf("\nplease enter the num between 1 and 4");
break;
}
}
printf("\nThe present data int queue are:");
if (Q == NULL) {
Q = (Queue*)malloc(sizeof(Queue));
if (Q == NULL) {
return 0;
}
Q = initQueue(Q);
printf("\n");
}
else {
current = Q->next;
while (current) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}
}
return 0;
}
4.不带头结点实现队列的增删(注意引用类型)
#include <stdio.h>
#include <malloc.h>
static int size = 0;
typedef struct Queue {
int value;
Queue* next;
}Queue,Qnode;
void initQueue(Queue*& Q) {
Q->value = 0;
Q->next = NULL;
}
int tailDequeue(Queue*& Q) {
if (size == 0) {
return 0;
}
else if(size == 1){
}
else{
Qnode* current;
current = Q;
while (current->next->next) {
current = current->next;
}
free(current->next);
current->next = NULL;
}
size--;
return 1;
}
int headDequeue(Queue*& Q) {
if (size == 0) {
return 0;
}
if (size == 1) {
}
else {
Q = Q->next;
}
size--;
return 1;
}
int tailEnqueue(Queue*& Q, int num) {
if (size == 0) {
Q->value = num;
}
else {
Qnode* current;
current = Q;
while (current->next) {
current = current->next;
}
Qnode* newnode;
newnode = (Qnode*)malloc(sizeof(Qnode));
if (newnode == NULL) {
return 0;
}
newnode->value = num;
newnode->next = NULL;
current->next = newnode;
}
size++;
return 1;
}
int headEnqueue(Queue*& Q,int num) {
if (size == 0) {
Q->value = num;
}
else {
Qnode* newnode;
newnode = (Qnode*)malloc(sizeof(Qnode));
if (newnode == NULL) {
return 0;
}
newnode->value = num;
newnode->next = Q;
Q = newnode;
}
size++;
return 1;
}
int main() {
Queue *Q;
int seq,num;
Q = (Queue*)malloc(sizeof(Queue));
if (Q == NULL) {
return 0;
}
initQueue(Q);
Qnode* current;
current = (Qnode*)malloc(sizeof(Qnode));
if (current == NULL) {
return 0;
}
initQueue(current);
while (true) {
printf("1.head enqueue\n2.head dequeue\n3.tail enqueue\n4.tail dequeue\n");
printf("Please select your operation:");
scanf_s("%d", &seq);
switch (seq) {
case(1): {
printf("please enter the num to headEnqueue: ");
scanf_s("%d", &num);
headEnqueue(Q, num);
break;
}
case(2): {
headDequeue(Q);
break;
}
case(3): {
printf("please enter the num to tailEnqueue: ");
scanf_s("%d", &num);
tailEnqueue(Q, num);
break;
}
case(4): {
tailDequeue(Q);
break;
}
default: {
printf("\nplease enter the num between 1 and 4");
break;
}
}
printf("\nThe present data int queue are:");
if (size == 0) {
printf("\n");
}
else {
current = Q;
while (current) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}
}
return 0;
}
5.总结
注意定义的size全局变量的++与--
可以用头节点的value计数或定义大小
引用类型与普通指针类型的区分
边界值的界定