链式栈
顾名思义这里的栈是与链表相挂钩,因为链表和栈有个共同的性质就是先进后出,所以写法与链表相似
基本思路
1.写出栈的数据结构
2.创建栈也就是初始化过程
3.写出节点的数据结构(因为入栈前得要用东西可以入到栈里面)
4.创建节点
5.入栈(也就是插入节点写法与链表相同)
6.获取栈顶元素
7.出栈(也就是删除节点写法与链表相同)
8.万金油函数,判断是否为空
代码
#include<stdio.h>
#include<stdlib.h>
#define Null 0
struct Node//节点数据结构类型
{
int date;
struct Node* next ;
};
//创建节点
struct Node* createNode(int date)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = Null;
newNode->date = date;
return newNode;
}
struct stack //栈数据结构类型
{
struct Node* stacktop;//栈顶标记
int size;//栈中元素的个数
};
//创建栈
struct stack* createstack() //创建过程就是初始化过程
{
struct stack* mystack = (struct stack*)malloc(sizeof(struct stack));
mystack->stacktop = Null;
mystack->size = 0;
return mystack;
}
//插入节点
void push(struct stack* mystack,int date)
{
struct Node* newNode = createNode(date);//想要插入栈必须现有节点所以先创建一个节点
//入栈操作就是链表表头插入
newNode->next = mystack->stacktop;
mystack->stacktop = newNode;
mystack->size++;
}
//获取栈顶元素
int top(struct stack* mystack)
{
//防御编程,防止栈为空程序运行崩溃
if(mystack->size == 0)
{
printf("栈为NUll,无法h获取栈顶元素!\n");
system("pause");
return mystack->size ;
}
return mystack->stacktop->date ;
}
//出栈 就是链表的删除
void pop(struct stack* mystack)
{
if(mystack->size == 0)
{
printf("栈为NUll,无法h获取栈顶元素!\n");
system("pause");
return mystack->size;
}
else
{
struct Node* nextNode = mystack->stacktop->next;
free(mystack->stacktop);
mystack->stacktop=nextNode ;
mystack->size--;
}
}
//万金油函数判断栈是不是为空
int empty(struct stack* mystack)
{
if(mystack->size == 0)
return 0;
return 1;
}
int main()
{
struct stack* mystack= createstack();//创建栈
//入栈
push(mystack,1);
push(mystack,2);
push(mystack,3);
while(empty(mystack))//当栈不为空的时候
{
printf("%d\n",top(mystack));
pop(mystack);
}
printf("\n");
system("pause");
return 0;
}
遇到的困难
刚开始对于数据结构很不熟悉,但是因为熟能生巧嘛,学会了单项链表和双向链表,在学习链式栈就轻松很多,也能马上理解
想法
同理可以得出链式队列方法一样
代码如下
#include<stdio.h>
#include<stdlib.h>
#define Null 0
struct Node
{
int date;
struct Node* next;
};
struct Node* createNode(int date)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->date= date;
newNode->next =Null;
return newNode;
}
struct queue
{
struct Node* frontNode;
struct Node* tailNode;
int size;
};
struct queue* createqueue()
{
struct queue* myqueue = (struct queue*)malloc(sizeof(struct queue));
myqueue->tailNode = myqueue->frontNode = Null;
myqueue->size = 0 ;
return myqueue;
}
void push(struct queue* myqueue,int date)//入队
{
struct Node* newNode = createNode(date);
if(myqueue->size==0)//防止为空,这个细节很重要
{
myqueue->frontNode = myqueue->tailNode = newNode;
}
else
{
myqueue->tailNode->next=newNode;//连接一个新的节点
myqueue->tailNode=newNode;//队尾改成新节点
}
myqueue->size++;
}
void pop(struct queue* myqueue)//出队
{
if(myqueue->size==0)
{
printf("队伍为NUll,无法出队!\n");
system("pause");
return ;
}
else
{
struct Node* nextNode= myqueue->frontNode->next;
free(myqueue->frontNode);
myqueue->frontNode = nextNode;
myqueue->size--;
}
}
int front (struct queue* myqueue)
{
if(myqueue->size==0)
{
printf("队伍为NUll,无法出队!\n");
system("pause");
return myqueue->size;
}
return myqueue->frontNode->date;
}
int empty(struct queue* myqueue)
{
if(myqueue->size == 0)
return 0;
return 1;
}
int main()
{
struct queue* myqueue = createqueue();
push(myqueue,1);
push(myqueue,2);
push(myqueue,3);
while(empty(myqueue))
{
printf("%d\n",front(myqueue));
pop(myqueue);
}
printf("\n");
system("pause");
return 0;
}