信管1172唐杰数据结构实验二

以下依次为顺序栈、链栈、顺序队列、链队列

#include<iostream.h>

class stack

{

int top;

int *stackArray;

int size;

public:

stack(int MaxSize)

{

stackArray=new int[MaxSize];

top=-1;

size=MaxSize;

}

~stack()

{

delete[] stackArray;

}

void push(int x)

{

if(top==size-1)

{

cout<<"上溢"<<endl;

return;

}

top++;

stackArray[top]=x;

}

void pop()

{

if(top==-1)

{

cout<<"下溢"<<endl;

return;

}

cout<<"弹出:"<<stackArray[top]<<endl;

top--;

}

};

int main()

{

stack a(10);

for(int i=1;i<=11;i++)

 a.push(i);

for(i=1;i<=11;i++)

a.pop();

return 0;

}

 


#include<iostream.h>

struct node

{

int date;

node *next;

};

class ls

{

node *top;

public:

ls()

{

top=NULL;

}

~ls()

{

node *ptr=new node;

while(top!=NULL)

{

ptr=top;

top=top->next;

delete ptr;

}

}

void push(int var)

{

node *ptr=new node;

ptr->date=var;

ptr->next=top;

top=ptr;

}

void pop()

{

cout<<"弹出:"<<top->date<<endl;

node *ptr=top;

top=top->next;

delete ptr;

}

int stackTop()

{

return top->date;

}

};

int main()

{

ls zhan;

for(int i=1;i<=10;i++)

zhan.push(i);

cout<<"栈顶元素为:"<<zhan.stackTop()<<endl;

    for(i=1;i<=10;i++)

zhan.pop();

return 0;

}

#include<iostream.h>

class queue

{

int front;

int rear;

int *array;

int size;

public:

queue(int MaxSize)

{

array=new int[MaxSize+1];

front=rear=0;

size=MaxSize;

}

~queue()

{

delete[] array;

}

void push(int x)

{

if(front==rear%size+1)

{

cout<<"上溢"<<endl;

return;

}

if(front==0)

front=1;

rear=rear%size+1;

array[rear]=x;

}

void pop()

{

if(front==0)

{

cout<<"下溢"<<endl;

return;

}

cout<<"弹出:"<<array[front]<<endl;

if(front==rear)

{

front=rear=0;

return;

}

front=front%size+1;

}

};

int main()

{

queue a(10);

for(int i=1;i<=11;i++)

a.push(i);

for(i=1;i<=11;i++)

a.pop();

return 0;

}

 


#include<iostream.h>

struct node

{

int date;

node *next;

};

class lq

{

node *front;

node *rear;

public:

lq()

{

node *s=new node;

s->next=NULL;

front=rear=s;

}

~lq()

{

node *s=NULL;

while(front->next!=NULL)

{

s=front;

front=front->next;

delete s;

}

}

void eq(int x)

{

node *s=new node;

s->date=x;

s->next=NULL;

rear->next=s;

rear=s;

}

void dq()

{

if(front==rear)

{

cout<<"下溢"<<endl;

return;

}

node *s=NULL;

node *d=new node;

d->next=NULL;

s=front->next;

if(front->next==rear)

front=rear=d;

else

front->next=s->next;

cout<<"弹出:"<<s->date<<endl;

delete s;

}

};

int main()

{

lq duilie;

for(int i=1;i<=10;i++)

duilie.eq(i);

    for(i=1;i<=11;i++)

duilie.dq();

return 0;

}

 


猜你喜欢

转载自blog.csdn.net/q1272211293/article/details/80154787