栈是一种数据结构,它是一种只能在一端进行插入或删除操作的线性表。栈的特点是后进先出,即最后入栈的元素最先出栈。
栈的基本操作包括:
C语言实现:
#include
#include
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int top;
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 判栈空
int isEmpty(Stack *s) {
return s->top == -1;
}
// 进栈
void push(Stack *s, int value) {
if (s->top == MAXSIZE - 1) {
printf("Stack overflow\n");
return;
}
s->data[++s->top] = value;
}
// 出栈
int pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack underflow\n");
exit(1);
}
return s->data[s->top--];
}
// 读栈顶元素
int top(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
exit(1);
}
return s->data[s->top];
}
int main() {
Stack s;
initStack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("Top element: %d\n", top(&s));
printf("Pop: %d\n", pop(&s));
printf("Top element: %d\n", top(&s));
return 0;
}
Python实现:
class Stack:
def __init__(self):
self.data = []
def is_empty(self):
return len(self.data) == 0
def push(self, value):
self.data.append(value)
def pop(self):
if self.is_empty():
print("Stack underflow")
return
return self.data.pop()
def top(self):
if self.is_empty():
print("Stack is empty")
return
return self.data[-1]
s = Stack()
s.push(1)
s.push(2)
s.push(3)
print("Top element:", s.top())
print("Pop:", s.pop())
print("Top element:", s.top())
共享栈是一种数据结构,它是两个栈共享同一片内存空间的一种实现方式。在共享栈中,两个栈的栈底分别位于共享空间的两端,它们向中间生长,当它们的栈顶指针相遇时,表示栈满。
共享栈的实现方式有多种,其中一种常见的方式是使用两个指针分别指向共享空间的两端,当两个栈的元素个数相等时,它们的栈顶指针相遇。这种实现方式可以节省空间,但需要额外的逻辑来处理栈满和栈空的情况。
共享栈可以用于实现两个栈共享同一片内存空间的场景,比如在编译器中用于实现函数调用的参数传递和返回值的存储。共享栈也可以用于解决空间限制的问题,比如在嵌入式系统中用于管理有限的内存空间。
C语言代码:
#include
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int top1;
int top2;
} ShareStack;
void initStack(ShareStack *stack) {
stack->top1 = -1;
stack->top2 = MAXSIZE;
}
int push(ShareStack *stack, int stackNum, int value) {
if (stack->top1 + 1 == stack->top2) {
printf("Stack is full\n");
return -1;
}
if (stackNum == 1) {
stack->data[++stack->top1] = value;
} else if (stackNum == 2) {
stack->data[--stack->top2] = value;
} else {
printf("Invalid stack number\n");
return -1;
}
return 0;
}
int pop(ShareStack *stack, int stackNum) {
if (stackNum == 1) {
if (stack->top1 == -1) {
printf("Stack 1 is empty\n");
return -1;
}
return stack->data[stack->top1--];
} else if (stackNum == 2) {
if (stack->top2 == MAXSIZE) {
printf("Stack 2 is empty\n");
return -1;
}
return stack->data[stack->top2++];
} else {
printf("Invalid stack number\n");
return -1;
}
}
int main() {
ShareStack stack;
initStack(&stack);
push(&stack, 1, 10);
push(&stack, 2, 20);
printf("Popped value from stack 1: %d\n", pop(&stack, 1));
printf("Popped value from stack 2: %d\n", pop(&stack, 2));
return 0;
}
Python代码:
class ShareStack:
def __init__(self):
self.data = [0] * 100
self.top1 = -1
self.top2 = 100
def push(self, stackNum, value):
if self.top1 + 1 == self.top2:
print("Stack is full")
return -1
if stackNum == 1:
self.top1 += 1
self.data[self.top1] = value
elif stackNum == 2:
self.top2 -= 1
self.data[self.top2] = value
else:
print("Invalid stack number")
return -1
return 0
def pop(self, stackNum):
if stackNum == 1:
if self.top1 == -1:
print("Stack 1 is empty")
return -1
value = self.data[self.top1]
self.top1 -= 1
return value
elif stackNum == 2:
if self.top2 == 100:
print("Stack 2 is empty")
return -1
value = self.data[self.top2]
self.top2 += 1
return value
else:
print("Invalid stack number")
return -1
stack = ShareStack()
stack.push(1, 10)
stack.push(2, 20)
print("Popped value from stack 1:", stack.pop(1))
print("Popped value from stack 2:", stack.pop(2))
链式存储栈是一种使用链表来实现的栈结构。在链式存储栈中,每个元素都是一个节点,节点包含数据和指向下一个节点的指针。链式存储栈可以动态地分配内存空间,不需要预先指定栈的大小,因此可以灵活地处理不同大小的数据。
C语言实现:
#include
#include
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Stack {
Node* top;
} Stack;
void push(Stack* stack, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
}
int pop(Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty\n");
return -1;
}
int value = stack->top->data;
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
return value;
}
int isEmpty(Stack* stack) {
return stack->top == NULL;
}
int top(Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty\n");
return -1;
}
return stack->top->data;
}
int main() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
push(stack, 1);
push(stack, 2);
push(stack, 3);
printf("Top element: %d\n", top(stack));
printf("Pop element: %d\n", pop(stack));
printf("Pop element: %d\n", pop(stack));
printf("Pop element: %d\n", pop(stack));
printf("Pop element: %d\n", pop(stack));
free(stack);
return 0;
}
Python实现:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.top = None
def push(self, value):
new_node = Node(value)
new_node.next = self.top
self.top = new_node
def pop(self):
if self.top is None:
print("Stack is empty")
return -1
value = self.top.data
self.top = self.top.next
return value
def is_empty(self):
return self.top is None
def get_top(self):
if self.top is None:
print("Stack is empty")
return -1
return self.top.data
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("Top element:", stack.get_top())
print("Pop element:", stack.pop())
print("Pop element:", stack.pop())
print("Pop element:", stack.pop())
print("Pop element:", stack.pop())
以上是今天要讲的内容,学到了栈相关的知识。