(1)头文件“SeqStack.h”
#ifndef SeqStack_H
#define SeqStack_H
const int StackSize=10;
class SeqStack
{
int data[StackSize];
int top;
public:
SeqStack();
~SeqStack(){;}
void Push(int x);
int Pop();
int GetTop();
int Empty();
};
#endif
(2)源程序文件“SeqStack..cpp”
#include"SeqStack.h"
template<class>
SeqStack::SeqStack()
{
top=-1;
}
template<class>
void SeqStack::Push(int x)
{
if(top==StackSize-1)throw"上溢";
top++;
data[top]=x;
}
template<class>
int SeqStack::Pop()
{
int x;
if(top==-1) throw"上溢";
x=data[top--];
return x;
}
template<class>
int SeqStack::GetTop()
{
if(top!=-1)
return data[top];
}
template<class>
int SeqStack::Empty()
{
if(top==-1)
return 1;
else return 0;
}
(3)源程序文件“SeqStack_main.cpp”
#include<iostream.h>
#include "SeqStack.cpp"
void main()
{
SeqStack S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对15和10执行入栈操作"<<endl;
S.Push(15);
S.Push(10);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
}
2.链栈的压栈和出栈
(1)头文件“LinkStack.h”
#ifndef LinkStack_H
#define LinkStack_H
template<class DataType>
struct Node
{
DataType data;
Node<DataType>*next;
};
template<class DataType>
class LinkStack
{
private:
Node<DataType>*top;
public:
LinkStack();
~LinkStack();
void Push(int x);
int Pop();
int GetPop();
int Empty();
};
#endif
(2)源程序文件“LinkStack.cpp”
#include"LinkStack.h"
template<class DataType>
LinkStack<DataType>::LinkStack()
{
top=NULL;
}
template<class DataType>
LinkStack<DataType>::~LinkStack()
{
Node<DataType>*q=new Node<DataType>;
while(top!=NULL)
{
q=top;
top=top->next;
delete q;
}
}
template<class DataType>
void LinkStack<DataType>::Push(int x)
{
Node<DataType> *s=new Node<DataType>;
s->data=x;
s->next=top;
top=s;
}
template<class DataType>
int LinkStack<DataType>::Pop()
{
int x;
Node<DataType> *p=new Node<DataType>;
if(top==NULL) throw"下溢";
x=top->data;
p=top;
top=top->next;
delete p;
return x;
}
template<class DataType>
int LinkStack<DataType>::GetPop()
{
if(top!=NULL)
return top->data;
}
template<class DataType>
int LinkStack<DataType>::Empty()
{
if(top==NULL)
return 1;
else return 0;
}
(3)源程序文件“LinkStack _main.cpp”
#include<iostream.h>
#include "LinkStack..cpp"
void main()
{
LinkStack<int> S;
if(S.Empty())
cout<<"链栈为空"<<endl;
else
cout<<"链栈非空"<<endl;
cout<<"对15和10执行入链栈操作"<<endl;
S.Push(15);
S.Push(10);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetPop()<<endl;
cout<<"执行一次出栈操作,出栈元素为:"<<endl;
cout<<S.Pop()<<endl;
cout<<"出栈后,栈顶元素为:"<<endl;
cout<<S.GetPop()<<endl;
}
3.(1)顺序队列的入队和出队:
#include<stdio.h>
#include<stdlib.h>
#defineQINITSIZE 100
#defineQINCRECEMENT 10
#defineSTATUS int
#definenull 0
#defineQElemType int
#defineOK 1
#defineERROR 0
typedefstruct QueueType{
QElemType*front;
QElemType*rear;
intqsize;
}queue;
{
q->front = (QElemType*)malloc(QINITSIZE*sizeof(QElemType));
if(q->front == null)return ERROR;q->rear = q->front;
q->qsize = QINITSIZE;
return OK;
}
STATUSEnqueue(queue *q,int e)
{
if(q->rear - q->front >=QINITSIZE)
{
q->front = (QElemType*)realloc(q,(q->qsize + QINCRECEMENT)*sizeof(QElemType));
q->rear = q->front;
q->qsize += QINCRECEMENT;
}
*q->rear++ = e;
return OK;
}
STATUSDequeue(queue *q,int *e)
{
if(q->rear == q->front)returnERROR;
*e = *q->front++;
q->qsize--;
return OK;
}
intmain()
{
queue q;
QElemType e,*p;
if(q_init(&q))printf("queueinitial OK!\n");
Enqueue(&q,1);
Enqueue(&q,2);
Enqueue(&q,3);
Enqueue(&q,4);
Enqueue(&q,5);
p = q.front;
printf("队列:");
while(p < q.rear)
printf("%d ",*p++);
Dequeue(&q,&e);
printf("删除元素为:");
printf("%d\n",e);
p=q.front;
printf("删除队头:");
while(p<q.rear)
printf("%d ",*p++);
printf("\n");
return 0;
}
4.链队列的入队和出队
(1)头文件“LinkQueue.h”
#ifndef LinkQueue_H
#define LinkQueue_H
template<class DataType>
struct Node
{
int data;
Node<int>*next;
};
template<class DataType>
class LinkQueue
{
public:
LinkQueue();
~LinkQueue();
void EnQueue(int x);
int DeQueue();
int GetQueue();
int Empty();
private:
Node<int>*front,*rear;
};
#endif;
(2)源程序文件“LinkQueue.cpp”
#include"LinkQueue.h"
template<class DataType>
LinkQueue<DataType>::LinkQueue()
{
Node<int>*s=NULL;
s=new Node<int>;
s->next=NULL;
front=rear=s;
}
template<class DataType>
LinkQueue<DataType>::~LinkQueue()
{
Node<int>*p=NULL;
while(front!=NULL)
{
p=front->next;
delete front;
front=p;
}
}
template<class DataType>
void LinkQueue<DataType>::EnQueue(int x)
{
Node<int>*s=NULL;
s=new Node<int>;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
template<class DataType>
int LinkQueue<DataType>::DeQueue()
{
Node<int>*p=NULL;
int x;
if(rear==front)throw"下溢";
p=front->next;
x=p->data;
front->next=p->next;
if(p->next==NULL)rear=front;
delete p;
return x;
}
template<class DataType>
int LinkQueue<DataType>::GetQueue()
{
if(front!=rear)
return front->next->data;
}
template<class DataType>
int LinkQueue<DataType>::Empty()
{
if(front==rear)
return 1;
else
return 0;
}
(3)源程序文件“LinkQueue_main.cpp”
#include<iostream.h>
#include"LinkQueue.cpp"
void main()
{
LinkQueue<int>Q;
if(Q.Empty())
cout<<"队列为空"<<endl;
else
cout<<"队列非空"<<endl;
cout<<"元素10和15执行入队操作:"<<endl;
try
{
Q.EnQueue(10);
Q.EnQueue(15);
}
catch(char*wrong)
{
cout<<wrong<<endl;
}
cout<<"查看队头元素:"<<endl;
cout<<Q.GetQueue()<<endl;
cout<<"执行出队操作"<<endl;
try
{
Q.DeQueue();
}
catch(char*wrong)
{
cout<<wrong<<endl;
}
cout<<"查看队头元素:"<<endl;
cout<<Q.GetQueue()<<endl;
}
5.十二进制转换为二进制
#include<iostream>
using namespace std;
const int MAX = 64;
void dec_to_bin(int);
void negative_dec_to_bin(int);
int main()
{
cout << "Enter a decimal number: ";
int dec_num;
cin >> dec_num;
if (dec_num < 0) negative_dec_to_bin(dec_num);
else if (dec_num == 0) cout << "The decimal number " << dec_num << " conversion to binary is: " << dec_num;
else dec_to_bin(dec_num);
cout << endl << "BYB!";
return 0;
}
void negative_dec_to_bin(int dec)
{
int temp;
temp = -dec;
int data[MAX];
int count = 0;
while (temp != 1)
{
data[count] = temp % 2;
count++;
temp = temp / 2;
}
data[count] = 1;
for(int i=count;i>=0;i--)
{
if (data[i] == 0) data[i] = 1;
else data[i] = 0;
}
for(int j=count;j>=0;j--)
{
if (data[j] == 0) { data[j] = 1; break; }
if (data[j] == 1) data[j] = 0;
}
for (int k = 0; k <= count; k++)
cout << data[k ];
}
void dec_to_bin(int dec)
{
int temp;
temp = dec;
int data[MAX];
int count = 0;
while(temp!=1)
{
data[count] = temp % 2;
count++;
temp = temp / 2;
}
data[count] = 1;
for(;count>=0;count--)
cout << data[count];
}