主题:新手写的简单的栈和队列的类定义(C++)望大虾指点
栈####################################################
#include <iostream.h>
struct Node{
int data;
struct Node *next;
};
class Stack
{
Node *top;
//Node *tail; 队列
public:
Stack()
{
p=new Node;
p->next=NULL;
top=p;
}
void Push(int el);
int Pop();
int Getpop();
~Stack();
};
void Stack::Push(int el)
{
Node *n;
n=new Node;
n->data=el;
n->next=top;
top=n;
}
int Stack::Pop()
{
int el;
if(top==NULL)
return 0;
else
{
Node *temp;
el=top->data;
temp=top;
delete temp;
temp=NULL;
top=top->next;
return el;
}
}
int Stack::Getpop()
{
if(top==NULL)
return 0;
else
return (top->data);
}
Stack::~Stack()
{
Node *p;
while(top!=NULL)
{
p=top;
top=top->next;
delete p;
p=NULL;
}
}
队列#####################################
#include <iostream.h>
struct Node
{
int data;
Node *next,*pre;
};
class Queue
{
Node *head,*rear;
public:
Queue()
{
Node *p;
p=new Node;
p->next=NULL;
rear=head=p;
}
bool AddNode(int el);
int DeNode();
~Queue()
{
Node *d;
while(head->next!=NULL)
{
d=head;
head=head->next;
delete d;
d=NULL;
}
}
};
bool Queue::AddNode(int el)
{
Node *newnode;
newnode=new Node;
newnode->data=el;
if(head!=NULL)
{
newnode->next=head;
head=newnode;
return true;
}
else return false;
}
int Queue::DeNode()
{
int elemtp;
Node *p,*temp;
p=head;
while(p->next!=rear) p=p->next;
temp=rear;
elemtp=temp->data;
rear=p;
delete temp;
temp=NULL;
return elemtp;
}
#include <iostream.h>
struct Node{
int data;
struct Node *next;
};
class Stack
{
Node *top;
//Node *tail; 队列
public:
Stack()
{
p=new Node;
p->next=NULL;
top=p;
}
void Push(int el);
int Pop();
int Getpop();
~Stack();
};
void Stack::Push(int el)
{
Node *n;
n=new Node;
n->data=el;
n->next=top;
top=n;
}
int Stack::Pop()
{
int el;
if(top==NULL)
return 0;
else
{
Node *temp;
el=top->data;
temp=top;
delete temp;
temp=NULL;
top=top->next;
return el;
}
}
int Stack::Getpop()
{
if(top==NULL)
return 0;
else
return (top->data);
}
Stack::~Stack()
{
Node *p;
while(top!=NULL)
{
p=top;
top=top->next;
delete p;
p=NULL;
}
}
队列#####################################
#include <iostream.h>
struct Node
{
int data;
Node *next,*pre;
};
class Queue
{
Node *head,*rear;
public:
Queue()
{
Node *p;
p=new Node;
p->next=NULL;
rear=head=p;
}
bool AddNode(int el);
int DeNode();
~Queue()
{
Node *d;
while(head->next!=NULL)
{
d=head;
head=head->next;
delete d;
d=NULL;
}
}
};
bool Queue::AddNode(int el)
{
Node *newnode;
newnode=new Node;
newnode->data=el;
if(head!=NULL)
{
newnode->next=head;
head=newnode;
return true;
}
else return false;
}
int Queue::DeNode()
{
int elemtp;
Node *p,*temp;
p=head;
while(p->next!=rear) p=p->next;
temp=rear;
elemtp=temp->data;
rear=p;
delete temp;
temp=NULL;
return elemtp;
}