|
线性表:
/*****链式表的实现和操作*****/
//作者:zkkpkk
#include <iostream.h>
class Linklist//定义链表类
{
public:
Linklist* next;
Linklist()
{
next=NULL;
}
int data;
void Push(Linklist** refhead,int data);//添加节点
void Insert(Linklist* head,Linklist* pio);//插入节点
void Display(Linklist* head);//显示链表
int Length(Linklist* head);//统计节点数目
Linklist* Find(Linklist* head,int data);//查找节点地址
void Delete(Linklist* head,int data);//删除节点
};
Linklist* head=new Linklist;
void Linklist::Insert(Linklist* head,Linklist* pio)
{
Linklist *cur,*bef;
cur=bef=head;
while(cur != NULL)
{ if(cur->data >= pio->data)
break;
else
{bef=cur;
cur=cur->next;
}
}
if(cur==head)
{ pio->next=head;
head=pio;
}
else
{
bef->next=pio;
pio->next=cur;
}
}
Linklist* Linklist::Find(Linklist* head,int data)
{
bool bo=true;
Linklist* cur;
cur=head;
while(cur != NULL)
{
if(cur->data==data){
bo=false;
return cur;}
cur=cur->next;
}
if(bo==true){
cout<<"It is no have the Linklist!"<<endl;
return NULL;
}
}
int Linklist::Length(Linklist* head)
{
int count=0;
Linklist* cur;
cur=head;
while(cur != NULL)
{
cur=cur->next;
count++;
}
return count;
}
void Linklist::Push(Linklist** refhead,int data)
{
Linklist* newLinklist=new Linklist;
newLinklist->data=data;
newLinklist->next=*refhead;
*refhead=newLinklist;
}
void Linklist::Display(Linklist* head)
{
Linklist *temp=new Linklist;
temp=head;
if(temp==NULL)
cout<<"The Linklist is empty!"<<endl;
else
{
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<endl;
}
}
void Linklist::Delete(Linklist* head,int data)
{
Linklist *cur,*bef;
cur=bef=head;
while(cur != NULL)
{
if(cur->data==data)
break;
else
{
bef=cur;
cur=cur->next;
}
}
if(cur==head)
{
cur->next=head;
delete cur;
}
else
{
bef->next=cur->next;
delete cur;
}
}
栈:
/*****顺序栈的实现和操作*****/
//作者:zkkpkk
#include <iostream.h>
#define maxsize 64
class Stack//栈类型
{
public:
int data[maxsize];
int top;
int buttom;
Stack()
{
top=-1;
}
bool empty();//判断栈空
void Push(Stack* s,int data);//进栈
void Pop();//出栈
int Gettop(Stack* s);//取栈顶
};
Stack* s=new Stack;
bool Stack::empty()
{
if(s->top==s->buttom)
return true;
else
return false;
}
void Stack::Push(Stack* s,int data)
{
Stack* cur;
cur=s;
if(cur->top==maxsize-1)
{ cout<<"Over Flow!";
return;
}
else
{
s->top++;
s->data[s->top]=data;
}
}
void Stack::Pop()
{
if(s->empty()==true)
{
cout<<"Under Flow!";
return;
}
else
{
s->top--;
s->data[s->top+1];
}
}
int Stack::Gettop(Stack* s)
{
Stack* cur;
cur=s;
if(cur->empty()==true)
{
cout<<"The Stack is empty!";
return 0;
}
else
return cur->data[cur->top];
}
/*****共享顺序栈的实现与操作*****/
#include <iostream.h>
#define maxsize 64
class Stack//定义栈类
{
public:
int data[maxsize];
int top1,top2;
Stack()
{
top1=-1;
top2=maxsize;
}
void Push(Stack* s,char *ch,int data);//进栈
void Pop(Stack* s,char *ch);//出栈
};
Stack* s=new Stack;
void Stack::Push(Stack* s,char *ch,int data)
{
Stack* cur;
cur=s;
if(cur->top2-cur->top1==1)
{
cout<<"The Stack is fall!";
return;
}
else
{
if(ch=="s1")
{
cur->top1=cur->top1+1;
cur->data[cur->top1]=data;
return;
}
if(ch=="s2")
{
cur->top2=cur->top2-1;
cur->data[cur->top2]=data;
return;
}
}
}
void Stack::Pop(Stack* s,char *ch)
{
Stack* cur;
cur=s;
if(ch=="s1")
{
if(cur->top1<0)
{
cout<<"The Stack is empty!";
return;
}
else
{
cur->top1--;
cur->data[cur->top1+1];
}
}
if(ch=="s2")
{
if(cur->top2>maxsize-1)
{
cout<<"The Stack is empty!";
return;
}
else
{
cur->top2++;
cur->data[cur->top2-1];
}
}
} |
|