首页 > 其他分享 >链栈

链栈

时间:2024-05-26 17:12:48浏览次数:26  
标签:LinkStack int top next 链栈 pnode data

链栈

  1. linkStack.h文件
#pragma once

typedef struct Node {
	int data;
	struct Node* next;
}node, * pnode;

class LinkStack {
private:
	Node* top;
public:
	LinkStack();
	~LinkStack();
	void push(int x);
	int pop();
	int getTop();
	bool isEmpty();
	void display();
};
  1. LinkStack.cpp文件
#include<iostream>
#include"LinkList.h"

LinkStack::LinkStack() {
	top = NULL;
}

LinkStack::~LinkStack() {
	while (top) {
		struct Node* q = top;
		top = top->next;
		delete q;
	}
}


void LinkStack:: push(int x){
	 pnode p = new node;
	 p->data = x;
	 p->next = top->next;
	 top->next = p;
}


int LinkStack::pop() {
	int temp = top->data;
	pnode q = top;
	top = top->next;
	delete q;
	return temp;
}


int LinkStack::getTop() {
	int temp = top->data;
	return temp;
}

bool LinkStack::isEmpty() {
	if (top == NULL) {
		return 1;
	}
	else {
		return 0;
	}
}

void LinkStack::display() {
	pnode p = top;
	while (p) {
		cout << p->data;
		p = p->next;
	}
}

  1. 测试文件
#include<iostream>
#include"LinkStack.h"
using namespace std;
int main() {
	LinkStack st1;
	int n = 0;
	cout << "输入入栈数据个数" << endl;
	cin >> n;
	int* number = new int[n];
	for (int i = 0; i < n; i++)
		cin >> number[i];
	int i = 0;
	while (i < n) {
		st1.push(number[i]);
		i++;
	}
	st1.display();

	int count = st1.pop();
	st1.display();
}

问题总结

  • 头文件格式
   //Circle_queue 头文件名
   #ifndef CIRCLE_QUEUE_H //就是头文件名(全大写后加个_H)

   #define CIRCLE_QUEUE_H
   //代码...
   #endif
  • 类的私有成员top是指针,用来指向链表的第一个元素(无头结点),但是再未指向之前,是个空指针,如果用以下方式进行入栈操作会发生错误
    void LinkStack::push(int x) {
	pnode p = new node;
	p->data = x;
	p->next = top->next;
	top = p;
}

标签:LinkStack,int,top,next,链栈,pnode,data
From: https://www.cnblogs.com/Dllsss/p/18213956

相关文章

  • 链栈的接口程序
    /***************************************************filename:LkStack.c*author:[email protected]*date:2024/04/25*brief:构建链栈*note:None**CopyRight(c)[email protected]********......
  • 链栈的接口设计
    #include<stdio.h>#include<stdbool.h>#include<stdlib.h>//指的是顺序栈中的元素的数据类型,用户可以根据需要进行修改typedefintDataType_t;//构造记录顺序栈SequenceStack各项参数(栈底地址+栈容量+栈顶元素的下标)的结构体typedefstructLinkStack{DataTyp......
  • 顺序栈和链栈的部分功能完整代码
    一、顺序栈代码#include<iostream>#include<stdlib.h>usingnamespacestd;#defineOK1#defineERROR0#defineMAXSIZE100typedefintElemType;typedefintStatus;typedefstruct {   ElemType*elem;   inttop;}Sqstack;StatusInitStack(Sqsta......
  • 2008秋-链栈-链式存储结构的栈
    /*---------------------------------------------------------Title:LinkStack(链栈)链栈-链式存储结构的栈请先阅读教材67页,2.3.2,2.3.4节,栈的定义及基本运算(注意以下程序为简化后的,仅供入门学习之用)----------------------------------------------------------......
  • 链栈的定义、初始化、出栈、入栈等操作
    #include<iostream>usingnamespacestd;/*链栈的定义*/typedefstructsNode{chardata;structsNode*next;}sNode;typedefsNode*linkStack;/*初始化链栈*/voidinitStack_L(linkStack&S){S=newsNode;S->next=NULL;}/*建立一个链栈......
  • 2023-03-30-链栈LinkStack的实现
    1#include<stdio.h>2#include<malloc.h>34typedefstructSNode5{6intdata;7structSNode*next;8}SNode,*LinkStack;9//栈的主要操作是在栈顶进行插入和删除,所以将链表的头部看为栈顶最合适10voidinitLinkStack(LinkStackS)//初始化1......
  • 3.5 链栈的表示和实现
    链栈的表示链栈是运算受限的单链表,只能在链表头部进行操作typedefstructStackNode{ SElemTypedata;StructStackNode*next;}StackNode,*LinkStack;LinkSta......
  • C/C++ 数据结构链栈的基本操作实现
    #include<iostream>#include<string.h>usingnamespacestd;typedefintSElemType;typedefstructStackNode{SElemTypedata;structStackNode*next;......
  • 链栈
    #include<iostream>usingnamespacestd;typedefstructStackNode{ intdata; structStackNode*next;}StackNode,*LinkStack;......
  • C++分别用顺序栈和链栈实现数制的转换相关代码
    //案例分析:将一个十进制数N转化为八进制数,在计算过程中,使得N模8得到八进制数的各个数依次进栈,//然后将八进制数依次输出,得到八进制数。#include<iostream>#include<cstdlib......