栈
#include<iostream>
using namespace std;
#define ok 1
#define error 0
typedef struct{
int*base;//栈底指针
int*top;//栈顶指针
int stacksize;//开辟空间长度
int length;//数组长度
}stack;
int init(stack&s)//初始化
{s.base=new int[20];//开辟空间
s.top=s.base;//初始化栈底与栈顶指针相等
s.stacksize=20;//空间长度赋值
s.length=0;
return ok;
}
int push(stack&s,int e)//入栈
{if(s.top-s.base==s.stacksize){//如果栈已满
return error;
}
*s.top=e;//栈顶入值
s.top++;//栈顶指针加一
s.length++;
return ok;
}
int del(stack&s,int&e)//出栈
{if(s.top==s.base){//如果栈长度为0
return error;
}
e=*s.top;//用e存储栈顶元素
s.top--;//出栈,栈顶指针减一
s.length--;
return ok;
}
void show(stack&s)//显示
{cout<<"长度为"<<s.length<<endl;
int*p=s.base;
for(int i=1;i<=s.length;i++){
cout<<*p++<<endl;
}
}
int main(){
stack s;
int e;
init(s);
for(int i=1;i<=10;i++){
push(s,i);
}
show(s);
del(s,e);
show(s);
return 0;
}
#include<iostream>
using namespace std;
#define ok 1
#define error 0
typedef struct node{
int data;
struct node*next;
}node,*link;
int init(link&s){
s=NULL;//不需要头节点
return ok;
}
int push(link&s,int e){
link p=new node;
p->data=e;
p->next=s;
s=p;
return ok;
}
int del(link&s,int&e){
if(!s)return error;
e=s->data;
node*p=s;
s=s->next;
delete p;
return ok;
}
int gettop(link&s){
if(s)
return s->data;
}
void show(link&s){
node*p=s;
int k=0;
while(p){
cout<<p->data<<endl;
p=p->next;
k++;
}
cout<<"长度为"<<k<<endl;
}
int main(){
link s;int e;
init(s);
for(int i=1;i<=10;i++){
push(s,i);
}
show(s);
del(s,e);
show(s);
cout<<gettop(s);
return 0;
}
队列
#include<iostream>
using namespace std;
#define ok 1
#define error 0
typedef struct queue{
int*data;
int front;//头指针
int rear;//尾指针
}quene;
int init(quene&q){//初始化
q.data=new int[20];
q.front=0;
q.rear=0;
return ok;
}
int len(quene&q){//长度
return (q.rear-q.front+20)%20;
}
int push(quene&q,int e){//入队
if ((q.rear + 1) % 20== q.front) return error;//队列已满
q.data[q.rear]=e;
q.rear=(q.rear+1)%20;
return ok;
}
int del(quene&q,int&e){//出队
if(q.front==q.rear)return error;//队列为空
else {
e=q.data[q.front];
q.front=(q.front+1)%20;
return ok;}
}
int gethead(quene&q){//得到队头元素
if(q.front!=q.rear)
return q.data[q.front];
}
int show(quene&q){
if(q.front==q.rear) return error;
if(q.front<q.rear){
for(int i=q.front;i<=q.rear-1;i++){
cout<<q.data[i]<<endl;
}
}
else{
for(int i=q.front;i<=19;i++){
cout<<q.data[i]<<endl;
}
for(int i=0;i<=q.rear-1;i++){
cout<<q.data[i]<<endl;
}
}
cout<<endl;
return ok;
}
int main(){
quene q;int e=0;
init(q);
for(int i=1;i<=19;i++){
push(q,i);
}
del(q,e);
push(q,1);
show(q);
return 0;
}
#include<iostream>
using namespace std;
#define ok 1
#define error 0
typedef struct node{
int data;
struct node*next;
}node,*quene;
typedef struct{
node*front;//头指针 指向头节点
node*rear;//尾指针
int length;
}link;
int init(link&q){//初始化
q.front=new node;
q.front->next=NULL;
q.rear=q.front;
q.length=0;
return ok;
}
int push(link&q,int e){//入队
quene point=new node;
point->data=e;
point->next=NULL;
q.rear->next=point;
q.rear=point;
q.length++;
return ok;
}
int del(link&q,int&e){//出队
if(q.front==q.rear)return error;//队为空
e=q.front->next->data;
quene point=q.front->next;
q.front->next=q.front->next->next;
if(point==q.rear){
q.rear=q.front;
}
delete point;
q.length--;
return ok;
}
int gethead(link&q){//得到队头
if(q.front==q.rear)return error;
return q.front->next->data;
}
void show(link&q){
cout<<"长度为"<<q.length<<endl;
quene point=q.front->next;
while(point){
cout<<point->data<<" ";
point=point->next;
cout<<endl;
}
}
int main(){
link q;int e;
init(q);
for(int i=1;i<=10;i++)
{push(q,i);}
del(q,e);
show(q);
return 0;
}