浙江大学数据结构陈越老师视频课程
链表代码整理
- 链表结构头文件
//structStack.h
#include <iostream>
#define MaxSize 10
#define ERROR -0.000000006
typedef struct SNode *Stack;
struct SNode{
double Data[MaxSize];
int Top=0;
};
//in stack
void Push(Stack PtrS, double item)
{
if(PtrS->Top==MaxSize-1){
printf("Stack is full.\n"); return;
}else {
PtrS->Data[++(PtrS->Top)]=item;
}
}
//out stack
double Pop(Stack PtrS)
{
if(PtrS->Top==-1){
printf("Stack is empty.\n");
return ERROR;
}else
{
return(PtrS->Data[(PtrS->Top)--]);
}
}
- 主函数,调用链表结构头文件,实现链表一系列具体操作。
//main.cpp
#include "structStack.h"
using namespace std;
int main()
{
int i=0;
Stack mystack=(Stack)malloc(sizeof(struct SNode));
cout<<"Push elements into the stack."<<endl;
for(i=1;i<11;i++)
{
double vluEle=3.0*i;
cout<<"Push "<<vluEle<<" into the stack."<<endl;
Push(mystack,vluEle);
}
cout<<"The length of stack is "<<mystack->Top<<endl;
cout<<"Pop elements from the stack:"<<endl;
double eleMent;
eleMent=Pop(mystack);
if(eleMent==ERROR){
cout<<"The stack is empty."<<endl;
}else{
cout<<"Pop "<<eleMent<<endl;
}
eleMent=Pop(mystack);
if(eleMent==ERROR){
cout<<"The stack is empty."<<endl;
}else{
cout<<"Pop "<<eleMent<<endl;
}
cout<<"Push new element into the stack"<<endl;
Push(mystack,9.9);
}
- 结果:
一个数组实现两个堆栈:
- 链表结构头文件
//structStack.h
#include <iostream>
#define MaxSize 10
#define ERROR -0.000000006
//an array realize two stacks
typedef struct DStack *Stack;
struct DStack{
double Data[MaxSize];
int Top1;
int Top2;
};
//push into stack, Tag: 1-> stack 1; 2-> stack 2
void Push(struct DStack *PtrS, double item, int Tag)
{
if(PtrS->Top2 - PtrS->Top1 ==1){
printf("The stack is full.\n"); return;
}
if(Tag==1)
{
PtrS->Data[++(PtrS->Top1)]=item;
}
else
{
PtrS->Data[--(PtrS->Top2)]=item;
}
}
//pop from stack
double Pop(struct DStack *PtrS, int Tag)
{
if(Tag==1){
if(PtrS->Top1==-1){
return ERROR;
}else return PtrS->Data[(PtrS->Top1)--];
}else{
if(PtrS->Top2==MaxSize){
return ERROR;
}else return PtrS->Data[(PtrS->Top2)++];
}
}
- 主函数,调用链表结构头文件,实现链表一系列具体操作。
//main.cpp
#include "structStack.h"
using namespace std;
int main()
{
int i=0;
Stack mystack = (Stack)malloc(sizeof(struct DStack));
mystack->Top1=-1;
mystack->Top2=MaxSize;
// cout<<"Push elements into the stack."<<endl;
for(i=1;i<11;i++)
{
double vluEle=3.0*i;
if(i%2==1){
cout<<"Push "<<vluEle<<" into stack 1."<<endl;
Push(mystack,vluEle, 1);
}
else{
cout<<"Push "<<vluEle<<" into stack 2."<<endl;
Push(mystack,vluEle, 2);
}
}
int lengthStack1= mystack->Top1 + 1;
int lengthStack2=MaxSize - mystack->Top2;
cout<<"The length of stack 1 is: "<<lengthStack1<<endl;
cout<<"The length of stack 2 is: "<<lengthStack2<<endl;
cout<<"Pop element from stack 1"<<endl;
double eleMent;
eleMent = Pop(mystack,1);
if(eleMent==ERROR){
cout<<"Stack 1 is empty."<<endl;
}else{
cout<<"Pop "<<eleMent<<endl;
}
cout<<"Pop element from stack 2"<<endl;
eleMent = Pop(mystack,2);
if(eleMent==ERROR){
cout<<"Stack 2 is empty."<<endl;
}else{
cout<<"Pop "<<eleMent<<endl;
}
cout<<"Push element into stack 2"<<endl;
Push(mystack, 9.6, 2);
cout<<"Push element into stack 2"<<endl;
Push(mystack, 3.6, 2);
lengthStack1=mystack->Top1 + 1;
lengthStack2=MaxSize - mystack->Top2;
cout<<"The length of stack 1 is: "<<lengthStack1<<endl;
cout<<"The length of stack 2 is: "<<lengthStack2<<endl;
cout<<"Pop element from stack 1"<<endl;
while (mystack->Top1!=-1)
{
eleMent=Pop(mystack,1);
if(eleMent==ERROR)
{
cout<<"Stack 1 is empty."<<endl;
}
else
{
cout<<"Pop "<<eleMent<<" from stack 1."<<endl;
}
}
eleMent=Pop(mystack,1);
if(eleMent==ERROR)
{
cout<<"Stack 1 is empty."<<endl;
}
else
{
cout<<"Pop "<<eleMent<<" from stack 1."<<endl;
}
cout<<"Pop element from stack 2"<<endl;
eleMent=Pop(mystack,2);
if(eleMent==ERROR)
{
cout<<"Stack 2 is empty."<<endl;
}
else
{
cout<<"Pop "<<eleMent<<" from stack 2."<<endl;
}
- 结果: