链栈代码实现及背包问题解决
2009-03-22 11:20
477 查看
//StackNode.h
#ifndef _STACKNODE
#define _STACKNODE
/*StackNode.h*/
#include <iostream>
using namespace std;
typedef int ElemType;
class StackNode
{
public:
ElemType data;
StackNode *next;
StackNode():data(NULL),next(NULL) { }
~StackNode(){ }
};
#endif
//LinkStack.h
#ifndef _LINKSTACK
#define _LINKSTACK
#include "StackNode.h"
class LinkStack
{
private:
StackNode *top;
public:
LinkStack(){}
~LinkStack(){}
void InitStack(LinkStack &);
bool IsEmpty(LinkStack);
int StackLength(LinkStack)const;
void Push(LinkStack &,ElemType);
bool Pop(LinkStack &,ElemType &);
void GetTop(LinkStack,ElemType &);
void StackDisplay(LinkStack);
};
#endif
//LinkStack.cpp
#include "LinkStack.h"
void LinkStack::InitStack(LinkStack &S)
{
top=NULL;
// S.top=NULL;
}
void LinkStack::Push(LinkStack &S,ElemType e)
{
StackNode *p=new StackNode;
p->data=e;
//栈底元素的后继为NULL
p->next=top;
//p->next=S.top;
//改变栈顶指针
top=p;
// S.top=p;
}
bool LinkStack::IsEmpty(LinkStack S)
{
if(this->top==NULL)
return true;
else
return false;
}
int LinkStack::StackLength(LinkStack S) const
{
StackNode *p=S.top;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
bool LinkStack::Pop(LinkStack &S,ElemType &e)
{
if(!IsEmpty(S))
{
StackNode *p=S.top;
//栈顶指针下移
S.top=S.top->next;
e=p->data;
//销毁原栈顶指针空间
delete p;
return true;
}
else
return false;
}
void LinkStack::GetTop(LinkStack S,ElemType &e)
{
StackNode *p=S.top;
e=p->data;
}
void LinkStack::StackDisplay(LinkStack S)
{
if(IsEmpty(S))
cout<<"该栈没有元素!"<<endl;
else
{
//从栈底到栈顶输出栈中的元素:
int n=S.StackLength(S);
ElemType *elem=new ElemType
;
StackNode *p=S.top;
for (int i=0;i<n;i++)
{
elem[i]=p->data;
p=p->next;
}
for(int i=n-1;i>=0;i--)
cout<<elem[i]<<endl;
}
}
//背包问题
#include "LinkStack.h"
#include <iostream>
using namespace std;
//利用栈的结构来处理背包问题
int main()
{
int w[6]={1,8,4,3,5,2};
int T=10;
int n=6;
LinkStack S;
S.InitStack(S);
int k=0;
do
{
while(T>0&&k<n)
{
if(T-w[k]>=0)
{
S.Push(S,k);
T=T-w[k];
}
k++;
}
if(T==0)
{
cout<<"输出一组背包问题的解:"<<endl;
S.StackDisplay(S);
}
S.Pop(S,k);
T=T+w[k];
k++;
}while(!S.IsEmpty(S)||k!=n);
return 0;
}
#ifndef _STACKNODE
#define _STACKNODE
/*StackNode.h*/
#include <iostream>
using namespace std;
typedef int ElemType;
class StackNode
{
public:
ElemType data;
StackNode *next;
StackNode():data(NULL),next(NULL) { }
~StackNode(){ }
};
#endif
//LinkStack.h
#ifndef _LINKSTACK
#define _LINKSTACK
#include "StackNode.h"
class LinkStack
{
private:
StackNode *top;
public:
LinkStack(){}
~LinkStack(){}
void InitStack(LinkStack &);
bool IsEmpty(LinkStack);
int StackLength(LinkStack)const;
void Push(LinkStack &,ElemType);
bool Pop(LinkStack &,ElemType &);
void GetTop(LinkStack,ElemType &);
void StackDisplay(LinkStack);
};
#endif
//LinkStack.cpp
#include "LinkStack.h"
void LinkStack::InitStack(LinkStack &S)
{
top=NULL;
// S.top=NULL;
}
void LinkStack::Push(LinkStack &S,ElemType e)
{
StackNode *p=new StackNode;
p->data=e;
//栈底元素的后继为NULL
p->next=top;
//p->next=S.top;
//改变栈顶指针
top=p;
// S.top=p;
}
bool LinkStack::IsEmpty(LinkStack S)
{
if(this->top==NULL)
return true;
else
return false;
}
int LinkStack::StackLength(LinkStack S) const
{
StackNode *p=S.top;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
bool LinkStack::Pop(LinkStack &S,ElemType &e)
{
if(!IsEmpty(S))
{
StackNode *p=S.top;
//栈顶指针下移
S.top=S.top->next;
e=p->data;
//销毁原栈顶指针空间
delete p;
return true;
}
else
return false;
}
void LinkStack::GetTop(LinkStack S,ElemType &e)
{
StackNode *p=S.top;
e=p->data;
}
void LinkStack::StackDisplay(LinkStack S)
{
if(IsEmpty(S))
cout<<"该栈没有元素!"<<endl;
else
{
//从栈底到栈顶输出栈中的元素:
int n=S.StackLength(S);
ElemType *elem=new ElemType
;
StackNode *p=S.top;
for (int i=0;i<n;i++)
{
elem[i]=p->data;
p=p->next;
}
for(int i=n-1;i>=0;i--)
cout<<elem[i]<<endl;
}
}
//背包问题
#include "LinkStack.h"
#include <iostream>
using namespace std;
//利用栈的结构来处理背包问题
int main()
{
int w[6]={1,8,4,3,5,2};
int T=10;
int n=6;
LinkStack S;
S.InitStack(S);
int k=0;
do
{
while(T>0&&k<n)
{
if(T-w[k]>=0)
{
S.Push(S,k);
T=T-w[k];
}
k++;
}
if(T==0)
{
cout<<"输出一组背包问题的解:"<<endl;
S.StackDisplay(S);
}
S.Pop(S,k);
T=T+w[k];
k++;
}while(!S.IsEmpty(S)||k!=n);
return 0;
}
相关文章推荐
- asp.net实现ftp上传代码(解决大文件上传问题)
- cnblogs 代码高亮显示后的代码复制问题解决实现代码
- asp.net实现ftp上传代码(解决大文件上传问题)
- asp.net实现ftp上传代码(解决大文件上传问题)
- 背包问题---01背包(原理,伪代码,编程实现)
- 背包问题----完全背包(详解|代码实现|背包具体物品的求解)
- 基本上,把switch,用设计模式代替,肯定是bug和过度设计。想想,本来修改一个文件几行代码可以解决的问题,变成修改3-6个类才能实现一样的功能。不是傻是什么?
- 不用写代码就能实现深度学习?手把手教你用英伟达 DIGITS 解决图像分类问题
- 房屋隔离问题(围栏问题)一种解决方法和实现代码。
- 在xib和story board上实现代码自动布局(解决添加约束时,大于等于小于等于繁琐的问题,以及占位view的问题)
- 背包问题---01背包(原理,伪代码,编程实现)
- 背包问题---01背包最优方案总数(原理剖析代码实现)
- 动态规划-背包问题九讲及其代码实现
- 【高级算法】Lasvegas算法解决3SAT问题(C++实现代码)
- Xcode .m实现文件 关键字没有提示,不提示代码补全功能,问题解决。
- java 实现新浪微博内容计数器 Java问题通用解决代码
- Java除法结果带小数、进一法的实现 Java问题通用解决代码
- eclipse使用github实现版本控制,轻松解决代码管理问题