栈
1.顺序存储结构
#include <iostream>
#include <string>
#define MAX 1024
using namespace std;
typedef int DataType;
class Stack
{
public :
Stack ( ) ;
DataType data[ MAX] ;
int top;
void show ( ) ;
int push ( DataType elem) ;
int pop ( DataType& elem) ;
} ;
Stack:: Stack ( )
{
this - > data[ MAX- 1 ] = 0 ;
this - > top = - 1 ;
}
void Stack:: show ( )
{
if ( this - > top == - 1 )
{
cout << "空栈" << endl;
}
else
{
cout << "栈中一共有" << this - > top + 1 << "个元素" << endl;
for ( int i = 0 ; i <= this - > top; i++ )
{
cout << this - > data[ i] << " " << ends;
}
cout << endl;
}
}
int Stack:: push ( DataType elem)
{
if ( this - > top == MAX - 1 )
{
cout << "栈已经满了" << endl;
return - 1 ;
}
this - > top++ ;
this - > data[ this - > top] = elem;
return 0 ;
}
int Stack:: pop ( DataType& elem)
{
if ( this - > top == - 1 )
{
cout << "空栈" << endl;
return - 1 ;
}
elem = this - > data[ this - > top] ;
this - > top-- ;
return 0 ;
}
int main ( )
{
int a;
Stack s;
Stack* ps = & s;
ps- > push ( 0 ) ;
ps- > push ( 1 ) ;
ps- > push ( 2 ) ;
ps- > push ( 3 ) ;
ps- > show ( ) ;
ps- > pop ( a) ;
ps- > show ( ) ;
cout << a << endl;
system ( "pause" ) ;
return 0 ;
}
2.链式存储结构
#include <iostream>
#include <string>
#define MAX 1024
using namespace std;
typedef int DataType;
class Node
{
public :
DataType data;
Node* next;
} ;
class Stack
{
public :
Stack ( ) ;
int count;
Node* top;
void show ( ) ;
int push ( DataType elem) ;
int pop ( DataType& elem) ;
} ;
Stack:: Stack ( )
{
this - > top = nullptr ;
this - > count = - 1 ;
}
void Stack:: show ( )
{
if ( this - > count == - 1 )
{
cout << "空栈" << endl;
}
else
{
cout << "一共有" << this - > count + 1 << "个元素" << endl;
for ( int i = 0 ; i <= this - > count; i++ )
{
cout << this - > top- > data << " " << ends;
this - > top = this - > top- > next;
}
cout << endl;
}
}
int Stack:: push ( DataType elem)
{
Node* newNode = new Node;
newNode- > data = elem;
newNode- > next = this - > top;
this - > top = newNode;
this - > count++ ;
cout << "添加成功" << endl;
return 0 ;
}
int Stack:: pop ( DataType& elem)
{
if ( this - > count == - 1 )
{
cout << "空栈" << endl;
return - 1 ;
}
elem = this - > top- > data;
this - > top = this - > top- > next;
this - > count-- ;
cout << "出栈成功" << endl;
return 0 ;
}
int main ( )
{
Stack s;
Stack* ps = & s;
int a;
ps- > push ( 0 ) ;
ps- > push ( 1 ) ;
ps- > push ( 2 ) ;
ps- > push ( 3 ) ;
ps- > push ( 4 ) ;
ps- > push ( 5 ) ;
ps- > pop ( a) ;
ps- > show ( ) ;
cout << a << endl;
system ( "pause" ) ;
return 0 ;
}
队列
1.循环队列
#include <iostream>
#include <string>
#define MAX 1024
using namespace std;
typedef int DataType;
class Queue
{
public :
DataType data[ MAX] ;
int front;
int rear;
Queue ( ) ;
int getQueueLenth ( ) ;
int enQueue ( DataType elem) ;
int deleteQueue ( DataType& elem) ;
} ;
Queue:: Queue ( )
{
this - > data[ MAX- 1 ] = 0 ;
this - > front = 0 ;
this - > rear = 0 ;
}
int Queue:: getQueueLenth ( )
{
return ( this - > rear - this - > front + MAX) % MAX;
}
int Queue:: enQueue ( DataType elem)
{
if ( this - > getQueueLenth ( ) == MAX- 1 )
{
cout << "满队" << endl;
return - 1 ;
}
this - > data[ this - > rear] = elem;
this - > rear = ( this - > rear + 1 ) % MAX;
cout << "入队成功" << endl;
return 0 ;
}
int Queue:: deleteQueue ( DataType& elem)
{
if ( this - > getQueueLenth ( ) == 0 )
{
cout << "空队" << endl;
return - 1 ;
}
elem = this - > data[ this - > front] ;
this - > front = ( this - > front + 1 ) % MAX;
cout << "出队成功" << endl;
return 0 ;
}
int main ( )
{
Queue q;
Queue* pq = & q;
int a;
pq- > enQueue ( 0 ) ;
pq- > enQueue ( 1 ) ;
pq- > enQueue ( 2 ) ;
cout << pq- > getQueueLenth ( ) << endl;
pq- > deleteQueue ( a) ;
cout << pq- > getQueueLenth ( ) << endl;
cout << "出队元素:" << a << endl;
system ( "pause" ) ;
return 0 ;
}
2.链式存储结构
#include <iostream>
#include <string>
#define MAX 1024
using namespace std;
typedef int DataType;
class Node
{
public :
DataType data;
Node* next;
} ;
class Queue
{
public :
Node* front;
Node* rear;
int count;
Queue ( ) ;
int enQueue ( DataType elem) ;
int deleteQueue ( DataType& elem) ;
} ;
Queue:: Queue ( )
{
Node* head = new Node;
head- > data = this - > count;
head- > next = nullptr ;
this - > front = head;
this - > rear = head;
this - > count = 0 ;
}
int Queue:: enQueue ( DataType elem)
{
Node* newNode = new Node;
newNode- > data = elem;
newNode- > next = nullptr ;
this - > rear- > next = newNode;
this - > rear = newNode;
this - > count++ ;
return 0 ;
}
int Queue:: deleteQueue ( DataType& elem)
{
if ( this - > count == 0 )
{
cout << "空队" << endl;
return - 1 ;
}
Node* temp;
temp = this - > front- > next;
elem = temp- > data;
this - > front- > next = temp- > next;
if ( temp == this - > rear)
this - > front = this - > rear;
delete temp;
this - > count-- ;
return 0 ;
}
int main ( )
{
Queue q;
Queue* pq = & q;
int a;
pq- > enQueue ( 0 ) ;
pq- > enQueue ( 1 ) ;
cout << pq- > count << endl;
pq- > deleteQueue ( a) ;
cout << pq- > count << endl;
system ( "pause" ) ;
return 0 ;
}