东莞资深网站建设,国外域名注册公司,手机网站模板下载免费,wordpress it在 C 编程语言中#xff0c;可以使用数组或链表实现堆栈。这两种实现都有其优点和注意事项#xff0c;因此让我们探讨这两种方法。
1. 使用数组的堆栈实现#xff1a; 在此实现中#xff0c;我们使用数组来表示堆栈。数组将具有固定大小#xff0c;变量将跟踪堆栈的顶…在 C 编程语言中可以使用数组或链表实现堆栈。这两种实现都有其优点和注意事项因此让我们探讨这两种方法。
1. 使用数组的堆栈实现 在此实现中我们使用数组来表示堆栈。数组将具有固定大小变量将跟踪堆栈的顶部元素。
c
#include stdio.h
#define MAX_SIZE 100 int stack[MAX_SIZE];
int top -1; // Function to check if the stack is empty
int isEmpty() { return (top -1);
} // Function to check if the stack is full
int isFull() { return (top MAX_SIZE - 1);
} // Function to push an element onto the stack
void push(int item) { if (isFull()) { printf(Stack Overflow\n); return; } stack[top] item;
} // Function to pop an element from the stack
int pop() { if (isEmpty()) { printf(Stack Underflow\n); return -1; // Return an invalid value or handle error appropriately } return stack[top--];
} // Function to get the top element of the stack
int peek() { if (isEmpty()) { printf(Stack is empty\n); return -1; // Return an invalid value or handle error appropriately } return stack[top];
} // Function to display the elements of the stack
void display() { if (isEmpty()) { printf(Stack is empty\n); return; } printf(Stack elements: ); for (int i top; i 0; i--) { printf(%d , stack[i]); } printf(\n);
} // Example usage
int main() { push(10); push(20); push(30); display(); // Output: Stack elements: 30 20 10 printf(%d\n, pop()); // Output: 30 printf(%d\n, peek()); // Output: 20 display(); // Output: Stack elements: 20 10 return 0;
} 2. 使用链表的堆栈实现 在此实现中链表用于表示堆栈。链表的每个节点都包含数据和指向下一个节点的指针。
c #include stdio.h
#include stdlib.h struct Node { int data; struct Node* next;
}; struct Node* top NULL; // Function to check if the stack is empty
int isEmpty() { return (top NULL);
} // Function to push an element onto the stack
void push(int item) { struct Node* newNode (struct Node*)malloc(sizeof(struct Node)); if (newNode NULL) { printf(Memory allocation failed\n); return; } newNode-data item; newNode-next top; top newNode;
} // Function to pop an element from the stack
int pop() { if (isEmpty()) { printf(Stack Underflow\n); return -1; // Return an invalid value or handle error appropriately } struct Node* temp top; int item temp-data; top top-next; free(temp); return item;
} // Function to get the top element of the stack
int peek() { if (isEmpty()) { printf(Stack is empty\n); return -1; // Return an invalid value or handle error appropriately } return top-data;
} // Function to display the elements of the stack
void display() { if (isEmpty()) { printf(Stack is empty\n); return; } printf(Stack elements: ); struct Node* current top; while (current ! NULL) { printf(%d , current-data); current current-next; } printf(\n);
} // Example usage
int main() { push(10); push(20); push(30); display(); // Output: Stack elements: 30 20 10 printf(%d\n, pop()); // Output: 30 printf(%d\n, peek()); // Output: 20 display(); // Output: Stack elements: 20 10 return 0;
} 这两种实现都提供了堆栈的基本操作“push”用于添加元素“pop”用于删除顶部元素“peek”用于检索顶部元素而不删除它“display”用于打印堆栈的元素。
在基于数组的实现中我们使用固定大小的数组并使用“top”变量跟踪顶部元素。函数“isEmpty”和“isFull”分别检查堆栈是空的还是满的以处理潜在的错误。“push”函数通过递增“top”并将值分配给数组中的相应索引来向堆栈添加元素。pop 函数通过递减 top 并从数组中返回值来删除 top 元素。“peek”函数在不修改堆栈的情况下返回顶部元素的值。“display”函数以相反的顺序遍历元素并打印它们。
在链表实现中我们为链表的节点定义了一个结构其中包含数据和指向下一个节点的指针。“top”指针指向堆栈中的第一个节点。“isEmpty”函数检查“top”指针是否为“NULL”以确定堆栈是否为空。“push”函数创建一个新节点分配数据并更新“next”指针以指向上一个顶部节点。“pop”函数删除顶部节点更新“top”指针并返回数据。“peek”函数返回顶部节点的数据而不修改堆栈。“display”函数遍历链表并打印每个节点的数据。
请记住根据特定要求适当地处理错误情况例如堆栈溢出或下溢。