如何用C语言链表实现模拟栈的数据结构实例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计625个文字,预计阅读时间需要3分钟。
C语言实现链表模拟栈的示例:
c#include #include
// 定义链表节点typedef struct Node { int data; struct Node *next;} Node;
// 创建链表模拟栈Node* createStack() { Node *top=NULL; return top;}
// 判断栈是否为空int isEmpty(Node *top) { return top==NULL;}
// 入栈void push(Node **top, int data) { Node *newNode=(Node*)malloc(sizeof(Node)); newNode->data=data; newNode->next=*top; *top=newNode;}
// 出栈int pop(Node **top) { if (isEmpty(*top)) { return -1; // 栈为空,返回错误码 } Node *temp=*top; int data=temp->data; *top=temp->next; free(temp); return data;}
// 主函数int main() { Node *stack=createStack(); push(&stack, 10); push(&stack, 20); push(&stack, 30);
printf(栈顶元素:%d\n, pop(&stack)); printf(栈顶元素:%d\n, pop(&stack));
if (isEmpty(stack)) { printf(栈为空\n); }
return 0;}
C语言数据结构之使用链表模拟栈的实例
以下是“使用链表模拟栈”的简单示例:
1. 用C语言实现的版本
#include<stdio.h> #include<stdlib.h> typedef char datatype; typedef struct node{ datatype data; struct node *next; } stack; stack* m_stack = NULL; /* 创建链表,从表头插入新元素 */ void creat(void); /* 使栈s为空 */ void MakeNull(); /* 判断栈是否为空 */ bool Empty(); //取出栈顶元素 int Top(datatype* value); //弹出栈顶元素 void Pop(); //入栈,在头部插入新元素 void Push(datatype x); /* 创建链表,从表头插入新元素 */ void creat() { char ch; printf("请输入字符序列:\n"); ch = getchar(); while (ch != '\n') { Push(ch); ch = getchar(); } } /* 使栈s为空 */ void MakeNull() { stack *p = m_stack; while (p != NULL) { m_stack = m_stack->next; free(p);/*释放空间*/ p = m_stack; } } /* 判断栈是否为空 */ bool Empty() { return (m_stack == NULL); } //取出栈顶元素 int Top(datatype* value) { if (Empty())/*s为空栈,直接跳出,提示出错信息*/ { return -1; } else { *value = m_stack->data; return 1; } } //弹出栈顶元素 void Pop() { stack *p; if (Empty()) /*s为空栈,直接跳出,提示出错信息*/ { printf("不能弹出,栈为空."); } else { p = m_stack;; m_stack = m_stack->next; free(p);/*释放栈顶空间*/ printf("弹出成功\n"); } } //入栈,在头部插入新元素 void Push( datatype x) { stack *p; p = (stack*)malloc(sizeof(stack)); p->data = x; p->next = m_stack; m_stack = p; } void main() { char m_top; /* 创建链表,从表头插入新元素 */ creat(); if (!Empty()) //判断栈是否为空 { int res = Top(&m_top); if (res == -1) { printf("栈为空,未能获取栈顶元素\n"); } else { printf("栈顶元素为: %c\n", m_top); } Pop(); } else { printf("栈为空\n"); } MakeNull(); }
运行结果如下图所示:
以上就是数据结构链表模拟栈的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文共计625个文字,预计阅读时间需要3分钟。
C语言实现链表模拟栈的示例:
c#include #include
// 定义链表节点typedef struct Node { int data; struct Node *next;} Node;
// 创建链表模拟栈Node* createStack() { Node *top=NULL; return top;}
// 判断栈是否为空int isEmpty(Node *top) { return top==NULL;}
// 入栈void push(Node **top, int data) { Node *newNode=(Node*)malloc(sizeof(Node)); newNode->data=data; newNode->next=*top; *top=newNode;}
// 出栈int pop(Node **top) { if (isEmpty(*top)) { return -1; // 栈为空,返回错误码 } Node *temp=*top; int data=temp->data; *top=temp->next; free(temp); return data;}
// 主函数int main() { Node *stack=createStack(); push(&stack, 10); push(&stack, 20); push(&stack, 30);
printf(栈顶元素:%d\n, pop(&stack)); printf(栈顶元素:%d\n, pop(&stack));
if (isEmpty(stack)) { printf(栈为空\n); }
return 0;}
C语言数据结构之使用链表模拟栈的实例
以下是“使用链表模拟栈”的简单示例:
1. 用C语言实现的版本
#include<stdio.h> #include<stdlib.h> typedef char datatype; typedef struct node{ datatype data; struct node *next; } stack; stack* m_stack = NULL; /* 创建链表,从表头插入新元素 */ void creat(void); /* 使栈s为空 */ void MakeNull(); /* 判断栈是否为空 */ bool Empty(); //取出栈顶元素 int Top(datatype* value); //弹出栈顶元素 void Pop(); //入栈,在头部插入新元素 void Push(datatype x); /* 创建链表,从表头插入新元素 */ void creat() { char ch; printf("请输入字符序列:\n"); ch = getchar(); while (ch != '\n') { Push(ch); ch = getchar(); } } /* 使栈s为空 */ void MakeNull() { stack *p = m_stack; while (p != NULL) { m_stack = m_stack->next; free(p);/*释放空间*/ p = m_stack; } } /* 判断栈是否为空 */ bool Empty() { return (m_stack == NULL); } //取出栈顶元素 int Top(datatype* value) { if (Empty())/*s为空栈,直接跳出,提示出错信息*/ { return -1; } else { *value = m_stack->data; return 1; } } //弹出栈顶元素 void Pop() { stack *p; if (Empty()) /*s为空栈,直接跳出,提示出错信息*/ { printf("不能弹出,栈为空."); } else { p = m_stack;; m_stack = m_stack->next; free(p);/*释放栈顶空间*/ printf("弹出成功\n"); } } //入栈,在头部插入新元素 void Push( datatype x) { stack *p; p = (stack*)malloc(sizeof(stack)); p->data = x; p->next = m_stack; m_stack = p; } void main() { char m_top; /* 创建链表,从表头插入新元素 */ creat(); if (!Empty()) //判断栈是否为空 { int res = Top(&m_top); if (res == -1) { printf("栈为空,未能获取栈顶元素\n"); } else { printf("栈顶元素为: %c\n", m_top); } Pop(); } else { printf("栈为空\n"); } MakeNull(); }
运行结果如下图所示:
以上就是数据结构链表模拟栈的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

