如何用C语言模板编写一个基础的栈类?

2026-05-08 17:163阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1289个文字,预计阅读时间需要6分钟。

如何用C语言模板编写一个基础的栈类?

本文以C语言为例,分享了使用模板实现简单栈类(数组版和链表版)的方法。以下为具体内容:

主要功能是实现一个后进先出(LIFO)的栈,基本操作包括:

- 入栈(Push):将元素添加到栈顶。- 出栈(Pop):移除并返回栈顶元素。- 返回栈顶元素(Peek):查看栈顶元素但不移除。- 判断栈是否为空(IsEmpty):检查栈中是否没有元素。- 获取栈的大小(Size):返回栈中元素的数量。

具体实现如下:

数组版栈

c#include #include #include

#define MAX_SIZE 100

typedef struct { int data[MAX_SIZE]; int top;} Stack;

void initStack(Stack *s) { s->top=-1;}

bool isFull(Stack *s) { return s->top==MAX_SIZE - 1;}

bool isEmpty(Stack *s) { return s->top==-1;}

void push(Stack *s, int value) { if (isFull(s)) { printf(Stack is full.\n); return; } s->data[++s->top]=value;}

int pop(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } return s->data[s->top--];}

int peek(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } return s->data[s->top];}

int size(Stack *s) { return s->top + 1;}

链表版栈

c#include #include #include

typedef struct Node { int value; struct Node *next;} Node;

typedef struct { Node *top;} Stack;

void initStack(Stack *s) { s->top=NULL;}

bool isFull(Stack *s) { // 对于链表版栈,通常不需要检查是否满 return false;}

bool isEmpty(Stack *s) { return s->top==NULL;}

void push(Stack *s, int value) { Node *newNode=(Node *)malloc(sizeof(Node)); newNode->value=value; newNode->next=s->top; s->top=newNode;}

int pop(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } Node *temp=s->top; int value=temp->value; s->top=s->top->next; free(temp); return value;}

int peek(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } return s->top->value;}

int size(Stack *s) { int count=0; Node *current=s->top; while (current !=NULL) { count++; current=current->next; } return count;}

以上代码展示了如何使用C语言实现数组版和链表版栈的基本操作。

本文实例为大家分享了C语言利用模板实现简单的栈类(数组和单链表),供大家参考,具体内容如下

主要的功能是实现一个后进先出的列表,有入栈、出栈、返回大小、判空等基本功能

#pragma once using namespace std; const int MAXSIZE = 0xfff; template<class type> class Class_Linkstack { int top; type* my_s; int max_size; public: Class_Linkstack() :top(-1), max_size(MAXSIZE) { my_s = new type[max_size]; if (my_s == NULL) { cerr << "动态存储分配失败!" << endl; exit(1); } } Class_Linkstack(int size) :top(-1), max_size(size) { my_s = new type[size]; if (my_s == NULL) { cerr << "动态存储分配失败!" << endl; exit(1); } } ~Class_Linkstack() { delete[] my_s; } bool Empty_Linkstack(); void Push_Linkstack(type tp); void Pop_Linkstack(); type Top_Linkstack(); int Size_Linkstack(); void Print_Linkstack(); }; template<class type> void Class_Linkstack<type>::Print_Linkstack() { if (top == -1) cout << "空栈" << endl; else { for (int i = 0; i < top+1; i++) cout << my_s[i] << '\t'; } } template<class type> bool Class_Linkstack<type>::Empty_Linkstack() { if (top == -1) return true; else { return false; } } template<class type> void Class_Linkstack<type>::Push_Linkstack(type tp) { if (top + 1 < max_size) my_s[++top] = tp; else { cout << "栈已满" << endl; exit(1); } } template<class type> void Class_Linkstack<type>::Pop_Linkstack() { if (top == -1) { cout << "为空栈" << endl; exit(1); } else { my_s[top--] = 0; } } template<class type> type Class_Linkstack<type>::Top_Linkstack() { if (top != -1) return my_s[top]; else { cout << "为空栈" << endl; exit(1); } } template<class type> int Class_Linkstack<type>::Size_Linkstack() { return top + 1; }

测试代码

#include "Class_Linkstack.h" int main() { Class_Linkstack<int> sk1(5); for (int i = 0; i < 5;i++ ) sk1.Push_Linkstack(i * 2 + 1); sk1.Print_Linkstack(); system("pause"); return 0; }

补充(通过单链表实现)

上面是通过数组来实现,与数组相比,链表实现更灵活,更容易增删元素。
单链表实现的核心思想是不断更新栈顶指针,来实现出栈压栈,每一个节点是一个结构体,包含一个value和一个next指针指向下一个元素,初始化时将栈顶指针置为NULL。

如何用C语言模板编写一个基础的栈类?

#pragma once using namespace std; template<class type> struct listnode { type value; listnode* next; listnode(type v,listnode* p):value(v),next(p){ } }; template<class type> class List_stack { listnode<type>* top; int size = 0; public: List_stack(); void Push(type &tp); void Pop(); bool Empty(); int Size(); void Print(); ~List_stack() { while (top) { listnode<type> * p = top; top = top->next; delete p; } } }; template<class type> bool List_stack<type>::Empty() { if (top == NULL) return true; else { return false; } } template<class type> List_stack<type>::List_stack() { top = NULL; size = 0; } template<class type> void List_stack<type>::Push(type &tp) { listnode<type> *tmp=new listnode<type>(tp,top); top = tmp; size++; } template<class type> void List_stack<type>::Pop() { if (top == NULL) { cout << "为空栈" << endl; } else { top = top->next; size--; } } template<class type> int List_stack<type>::Size() { return size; } template<class type> void List_stack<type>::Print() { listnode<type>* tmp = top; while (tmp != NULL) { cout << tmp->value << '\t'; tmp = tmp->next; } }

简单测试:

int main() { List_stack<int> ls; for (int i = 0; i < 5; i++) ls.Push(i); ls.Print(); ls.Pop(); ls.Pop(); cout << endl; ls.Print(); cout << endl; cout << ls.Size(); system("pause"); return 0; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计1289个文字,预计阅读时间需要6分钟。

如何用C语言模板编写一个基础的栈类?

本文以C语言为例,分享了使用模板实现简单栈类(数组版和链表版)的方法。以下为具体内容:

主要功能是实现一个后进先出(LIFO)的栈,基本操作包括:

- 入栈(Push):将元素添加到栈顶。- 出栈(Pop):移除并返回栈顶元素。- 返回栈顶元素(Peek):查看栈顶元素但不移除。- 判断栈是否为空(IsEmpty):检查栈中是否没有元素。- 获取栈的大小(Size):返回栈中元素的数量。

具体实现如下:

数组版栈

c#include #include #include

#define MAX_SIZE 100

typedef struct { int data[MAX_SIZE]; int top;} Stack;

void initStack(Stack *s) { s->top=-1;}

bool isFull(Stack *s) { return s->top==MAX_SIZE - 1;}

bool isEmpty(Stack *s) { return s->top==-1;}

void push(Stack *s, int value) { if (isFull(s)) { printf(Stack is full.\n); return; } s->data[++s->top]=value;}

int pop(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } return s->data[s->top--];}

int peek(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } return s->data[s->top];}

int size(Stack *s) { return s->top + 1;}

链表版栈

c#include #include #include

typedef struct Node { int value; struct Node *next;} Node;

typedef struct { Node *top;} Stack;

void initStack(Stack *s) { s->top=NULL;}

bool isFull(Stack *s) { // 对于链表版栈,通常不需要检查是否满 return false;}

bool isEmpty(Stack *s) { return s->top==NULL;}

void push(Stack *s, int value) { Node *newNode=(Node *)malloc(sizeof(Node)); newNode->value=value; newNode->next=s->top; s->top=newNode;}

int pop(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } Node *temp=s->top; int value=temp->value; s->top=s->top->next; free(temp); return value;}

int peek(Stack *s) { if (isEmpty(s)) { printf(Stack is empty.\n); return -1; } return s->top->value;}

int size(Stack *s) { int count=0; Node *current=s->top; while (current !=NULL) { count++; current=current->next; } return count;}

以上代码展示了如何使用C语言实现数组版和链表版栈的基本操作。

本文实例为大家分享了C语言利用模板实现简单的栈类(数组和单链表),供大家参考,具体内容如下

主要的功能是实现一个后进先出的列表,有入栈、出栈、返回大小、判空等基本功能

#pragma once using namespace std; const int MAXSIZE = 0xfff; template<class type> class Class_Linkstack { int top; type* my_s; int max_size; public: Class_Linkstack() :top(-1), max_size(MAXSIZE) { my_s = new type[max_size]; if (my_s == NULL) { cerr << "动态存储分配失败!" << endl; exit(1); } } Class_Linkstack(int size) :top(-1), max_size(size) { my_s = new type[size]; if (my_s == NULL) { cerr << "动态存储分配失败!" << endl; exit(1); } } ~Class_Linkstack() { delete[] my_s; } bool Empty_Linkstack(); void Push_Linkstack(type tp); void Pop_Linkstack(); type Top_Linkstack(); int Size_Linkstack(); void Print_Linkstack(); }; template<class type> void Class_Linkstack<type>::Print_Linkstack() { if (top == -1) cout << "空栈" << endl; else { for (int i = 0; i < top+1; i++) cout << my_s[i] << '\t'; } } template<class type> bool Class_Linkstack<type>::Empty_Linkstack() { if (top == -1) return true; else { return false; } } template<class type> void Class_Linkstack<type>::Push_Linkstack(type tp) { if (top + 1 < max_size) my_s[++top] = tp; else { cout << "栈已满" << endl; exit(1); } } template<class type> void Class_Linkstack<type>::Pop_Linkstack() { if (top == -1) { cout << "为空栈" << endl; exit(1); } else { my_s[top--] = 0; } } template<class type> type Class_Linkstack<type>::Top_Linkstack() { if (top != -1) return my_s[top]; else { cout << "为空栈" << endl; exit(1); } } template<class type> int Class_Linkstack<type>::Size_Linkstack() { return top + 1; }

测试代码

#include "Class_Linkstack.h" int main() { Class_Linkstack<int> sk1(5); for (int i = 0; i < 5;i++ ) sk1.Push_Linkstack(i * 2 + 1); sk1.Print_Linkstack(); system("pause"); return 0; }

补充(通过单链表实现)

上面是通过数组来实现,与数组相比,链表实现更灵活,更容易增删元素。
单链表实现的核心思想是不断更新栈顶指针,来实现出栈压栈,每一个节点是一个结构体,包含一个value和一个next指针指向下一个元素,初始化时将栈顶指针置为NULL。

如何用C语言模板编写一个基础的栈类?

#pragma once using namespace std; template<class type> struct listnode { type value; listnode* next; listnode(type v,listnode* p):value(v),next(p){ } }; template<class type> class List_stack { listnode<type>* top; int size = 0; public: List_stack(); void Push(type &tp); void Pop(); bool Empty(); int Size(); void Print(); ~List_stack() { while (top) { listnode<type> * p = top; top = top->next; delete p; } } }; template<class type> bool List_stack<type>::Empty() { if (top == NULL) return true; else { return false; } } template<class type> List_stack<type>::List_stack() { top = NULL; size = 0; } template<class type> void List_stack<type>::Push(type &tp) { listnode<type> *tmp=new listnode<type>(tp,top); top = tmp; size++; } template<class type> void List_stack<type>::Pop() { if (top == NULL) { cout << "为空栈" << endl; } else { top = top->next; size--; } } template<class type> int List_stack<type>::Size() { return size; } template<class type> void List_stack<type>::Print() { listnode<type>* tmp = top; while (tmp != NULL) { cout << tmp->value << '\t'; tmp = tmp->next; } }

简单测试:

int main() { List_stack<int> ls; for (int i = 0; i < 5; i++) ls.Push(i); ls.Print(); ls.Pop(); ls.Pop(); cout << endl; ls.Print(); cout << endl; cout << ls.Size(); system("pause"); return 0; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。