当前位置:   article > 正文

栈的顺序存储结构、链式存储架构及其实现_顺序栈和链栈的时间复杂度均为

顺序栈和链栈的时间复杂度均为

顺序栈和链式栈的时间复杂度均为O(1), 因此唯一可以比较的是空间性能。初始时顺序栈必须开辟一个固定的长度内存,所以存在可存储元素个数限制和浪费空间的问题。链式栈没有栈满的问题,只有当内存空间用完才会出现栈满的情况。但是每个元素都需要一个指针域,从而存在结构性开销。所以当栈的使用元素个数变化较大时,应采用链栈;反之应该采用顺序栈。

顺序存储结构及C语言实现
在这里插入图片描述

#include<iostream>
using namespace std;
#define StackSize 100
//定义数据类型
typedef int DataType;
//定义结构体
typedef struct 
{
    DataType data[StackSize];
    int top;
}SeqStack;
//初始化栈
void InitStack(SeqStack *S) {
    S->top = -1;                //要注意处置状态时栈顶位置为-1
}
//入栈操作
int Push(SeqStack *S, DataType x) {
    if(S->top == StackSize - 1) {
        printf("Failed, because the stack is full!\n");
        return 0;
    } 
    S->data[++S->top] = x;
    return 1;
}
//出栈操作
int Pop(SeqStack *S, DataType *ptr) {
    if(S->top == -1) {
        printf("Failed, because the stack is empty!\n");
        return 0;
    }
    *ptr = S->data[S->top--];
    return 1;
}
//获取栈顶元素
int GetTop(SeqStack *S, DataType *ptr) {
    if(S->top == -1) {
        printf("ERROR, the stack is empty, you can't get the top of the stack!\n");
        return 0;
    }
    *ptr = S->data[S->top];
    return 1;
}
//判断栈是否为空
int Empty(SeqStack *S) {
    if(S->top == -1)
        return 1;
    else
        return 0;
}
//主函数
int main() {
    DataType x;
    SeqStack S;
    InitStack(&S);      //初始化栈
    printf("We want to insert 10 and 15. \n");
    Push(&S, 10);       //元素10入栈
    Push(&S, 15);       //元素15入栈
    if(GetTop(&S, &x) == 1)         //打印栈顶元素
        printf("Now, the top element of the stack is %d \n", x);
    if(Pop(&S, &x) == 1)            //出栈
        printf("Doing a Pop, the element you delete is : %d\n", x);
    if(GetTop(&S, &x) == 1)         //打印栈顶元素
        printf("Now, the top element of the stack is %d \n", x);
    printf("you can push a new element into stack.\n");
    scanf("%d", &x);                //自定义元素入栈
    Push(&S, x);                    //然后输出栈顶元素
    if(Empty(&S) == 1)
        printf("your stack is empty!\n");
    else {
        printf("your stack isn't empty!\t");
        if(GetTop(&S, &x) == 1)
        printf("And the top element of the stack is %d \n", x);
    }
    system("pause");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

链式存储结构及C语言实现
在这里插入图片描述

#include<iostream>
using namespace std;
#define MaxSize 100
typedef int DataType;
//定义节点结构体
typedef struct Node
{
	DataType data;
	struct Node *next;
}Node;
//初始化
Node * InitStack() {
	Node *top = NULL;
	return top;
}
//销毁栈
void DestroyStack(Node *top) {
	Node *p = top;
	while (top != NULL) {
		printf("%d\t", top->data);
		top = top->next;
		free(p);
		p = top;
	}
}
//入栈操作
void Push(Node **top, DataType x) {
	Node *s = (Node *)malloc(sizeof(Node));
	s->data = x;
	s->next = *top;
	*top = s;
}
//出栈操作
int Pop(Node **top, DataType *ptr) {
	Node *p = *top;
	if (top == NULL) {
		printf("1ERROR, the stack is empty!\n");
		return 0;
	}
	*ptr = (*top)->data;
	*top = (*top)->next;
	free(p);
	return 1;
}
//获取栈顶元素
int GetTop(Node *top, DataType *ptr) {
	if (top == NULL) {
		printf("2ERROR, the stack is empty!\n");
		return 0;
	}
	*ptr = top->data;
	return 1;
}
//判断栈是否为空
int Empty(Node *top) {
	if (top == NULL)
		return 1;
	else
		return 0;
}
int main() {
	DataType x;
	Node *top = InitStack();
	printf("We want to insert 10 and 15. \n");
	Push(&top, 10);       //元素10入栈
	if (GetTop(top, &x) == 1)         //打印栈顶元素
		printf("Now, the top element of the stack is %d \n", x);
	Push(&top, 15);       //元素15入栈
	if (GetTop(top, &x) == 1)         //打印栈顶元素
		printf("Now, the top element of the stack is %d \n", x);
	if (Pop(&top, &x) == 1)            //出栈
		printf("Doing a Pop, the element you delete is : %d\n", x);
	if (GetTop(top, &x) == 1)         //打印栈顶元素
		printf("Now, the top element of the stack is %d \n", x);
	printf("you can push a new element into stack: \t");
	scanf("%d", &x);                //自定义元素入栈
	Push(&top, x);                    //然后输出栈顶元素
	if (Empty(top) == 1)
		printf("your stack is empty!\n");
	else {
		printf("your stack isn't empty!\t");
		if (GetTop(top, &x) == 1)
			printf("And the top element of the stack is %d \n", x);
	}
	DestroyStack(top);
	system("pause");
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/551892
推荐阅读
相关标签
  

闽ICP备14008679号