数据结构复习(-)线性结构
线性表链式存储实现
#include <iostream> using namespace std; struct Node //定义结点 { int data; Node *next; }; class Linklist //定义链表类 { public: Linklist(); //默认构造函数 Linklist(int a[],int n);//用数组初始化链表的构造函数 ~Linklist(); //析构函数 void Print(); //打印链表 int Length(); //求链表长度 int Getvalue(int i); //求链表中第i个结点的数据值 int Locate(int x); //求链表中值为x的结点的序号 void Insert(int i,int x); //在链表中第i个位置插入元素值为x的结点 int Delete(int i); //在链表中删除第i个结点 private: Node *first; }; Linklist::Linklist()//默认构造函数 { first=new Node; first->next=NULL; } Linklist::Linklist(int a[],int n)//数组初始化链表的构造函数,采用头插法 { first = new Node; //构造头结点 first->next = NULL; // Node* s; for(int i=0;i<n;i++) { s=new Node; s->data=a[i]; s->next=first->next; first->next=s; } } Linklist::Linklist(int a[],int n)//数组初始化链表的构造函数,采用尾插法 { first = new Node; first->next = NULL; Node *s,*p=first; for(int i=0;i<n;i++) { s=new Node; p->next=s; s->data=a[i]; p=s; } p->next=NULL; } Linklist::~Linklist()//析构函数 { Node *p=first; Node *tmp; while(p) { tmp=p; p=p->next; delete tmp; } cout<<"链表空间释放"<<endl; } int Linklist::Length()//计算链表长度(除去头结点) { Node *p=first->next; int n=0; while(p) { n++; p=p->next; } return n; } void Linklist::Insert(int i,int x)//向链表中插入元素 { Node *p=first; int j=1; while(j!=i&&p!=NULL) { p=p->next; j++; } if(p) { Node *s=new Node; s->data=x; s->next=p->next; p->next=s; } else cout<<"插入失败"<<endl; } int Linklist::Delete(int i)//在链表中删除序号为i的元素,并返回元素的值域 { Node *p=firstnext; int n,j=1; while(j!=i&&p->next!=NULL) { j++; p=p->next; } if(p->next!=NULL) { Node *s=p->next; p->next=s->next; n=s->data; delete s; return n; } else cout<<"删除失败"<<endl; } int Linklist::Getvalue(int i)//获取序号为i的元素的值 { Node *p=first->next; int j=1; while(j!=i&&p!=NULL) { p=p->next; j++; } if(p!=NULL) return p->data; else { cout<<"获取失败"<<endl; } } int Linklist::Locate(int x)//获取值为x的元素的序号 { Node *p=first->next; int n=1; while(p->data!=x&&p->next!=NULL) { n++; p=p->next; } if(p->next!=NULL) return n; else { cout<<"查找失败"<<endl; return -1; } } void Linklist::Print()//打印链表 { Node* head=first->next; while(head) { cout<<head->data<<" "; head=head->next; } cout<<endl; } int main() { int num[9]={3,2,5,6,9,40,87,809,92}; Linklist list(num,9); list.Print(); list.Insert(4,100); list.Print(); cout<<list.Delete(4)<<endl; list.Print(); cout<<list.Getvalue(4)<<endl; cout<<list.Locate(2)<<endl; cout<<list.Length()<<endl; return 0; }
栈链式存储实现
/************************************************************************* > File Name: LinkStack.cpp > Author: Shorey > Mail: shoreybupt@gmail.com > Created Time: 2015年03月23日 星期一 09时47分07秒 ************************************************************************/ #include<iostream> using namespace std; struct Node { int data; Node *next; }; class LinkStack { public: LinkStack() //构造函数 { top=NULL; } ~LinkStack(); //析构函数 void Push(int x); // 将x入栈 int Pop(); //弹出栈顶元素 int GetTop() //获取栈顶元素,但不删除 { if(top!=NULL) return top->data; } bool Empty() //判断栈是否为空 { if(top==NULL)return 1; else return 0; } private: Node *top; }; LinkStack::~LinkStack() { Node *p; while(top!=NULL) { p=top; top=top->next; delete p; } } void LinkStack::Push(int x) { Node *s=new Node; s->data=x; s->next=top; top=s; } int LinkStack::Pop() { Node *p; int x; if(top==NULL)cout<<"stack is empty"<<endl; else { x=top->data; p=top; top=p->next; delete p; return x; } } int main() { LinkStack s; s.Push(5); s.Push(6); s.Push(9); cout<<s.GetTop()<<endl; cout<<s.Pop()<<endl; cout<<s.GetTop()<<endl; return 0; }
队列链式存储实现
/************************************************************************* > File Name: LinkQueue.cpp > Author: Shorey > Mail: shoreybupt@gmail.com > Created Time: 2015年03月24日 星期二 15时31分18秒 ************************************************************************/ #include<iostream> using namespace std; struct Node { int data; Node *next; }; class LinkQueue { public: LinkQueue(); //构造函数,初始化一个空队列 ~LinkQueue(); //析构函数,释放队列中的结点 void EnQueue(int x); //把元素x入队 int DeQueue(); //把队头元素出队 int GetQueue() //获取队头元素,但不删除 { if(front!=rear) //判断队列不为空 return front->next->data; } bool Empty() //判断队列是否为空 { if(front==rear) return 1; else return 0; } private: Node *front,*rear; }; LinkQueue::LinkQueue() { front=new Node; front->next=NULL; rear=front; } LinkQueue::~LinkQueue() { Node *p=front; while(!front) { front=front->next; delete p; p=front; } } void LinkQueue::EnQueue(int x) { Node *s=new Node; s->data=x; s->next=NULL; rear->next=s; rear=s; } int LinkQueue::DeQueue() { if(!Empty()) { Node *p=front->next; int x; front->next=p->next; x=p->data; delete p; return x; } } int main() { LinkQueue s; s.EnQueue(4); s.EnQueue(6); s.EnQueue(7); cout<<s.DeQueue()<<endl; cout<<s.GetQueue()<<endl; return 0; }
队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。允许插入的一端为队尾(入队),允许删除(出队)的一端为队头。
顺序存储的队列是采用数组来实现的,但由于数组是静态的,在队列的出队和入队的操作下会出现整个队列后移逐渐占用下标加大位置而下标较小位置为空的“假溢出”现象,所以采用了将存储队列的数组看成是头尾相接的循环结构,即允许队列直接从数组的下标最大的位置延续到下标最小的位置。具体图示大家上网查查吧,在这里就不画了。
判断队列的状态的标准是头指针与尾指针的关系。约定队头指针front指向队头元素的前一个位置,队尾指针rear指向队尾元素。为了区分队满与队空,采取浪费一个数组元素的空间的策略:
队空:front==rear
队满:(rear+1)%QueueSize==front
具体实现如下:
/************************************************************************* > File Name: CirQueue.cpp > Author: Shorey > Mail: shoreybupt@gmail.com > Created Time: 2015年03月23日 星期一 14时38分12秒 ************************************************************************/ #include<iostream> using namespace std; const int QueueSize=100; class CirQueue { public: CirQueue() //构造函数,置空队列 { front=rear=0; } ~CirQueue(){cout<<"destory";} //析构函数 void EnQueue(int x); //元素x入队 int DeQueue(); //队头元素出队 int GetQueue(); //获取队头元素,不删除队头 bool Empty() //判断队列是否为空 { if(front==rear) return 1; else return 0; } private: int data[QueueSize]; //存放队列的数组 int front,rear; //头指针与尾指针 }; void CirQueue::EnQueue(int x) { if((rear+1)%QueueSize==front) //判断队列是否已满 cout<<"queue is full,can't put "<<x<<" into it"<<endl; else { rear=(rear+1)%QueueSize; //移动尾指针指向下一个空间 data[rear]=x; //元素x入队 } } int CirQueue::DeQueue() //队头元素出栈 { if(Empty()) //判断队列是否为空 cout<<"queue is empty"<<endl; else { front=(front+1)%QueueSize; //移动队头指针指向下一个空间,即被删元素所在位置 return data[front]; //返回被删除的元素的值 } } int CirQueue::GetQueue() { if(Empty()) cout<<"queue is empty"<<endl; else { return data[(front+1)%QueueSize]; } } int main() { CirQueue Q; Q.EnQueue(5); Q.EnQueue(9); Q.EnQueue(7); cout<<Q.DeQueue()<<endl; cout<<Q.GetQueue()<<endl; cout<<Q.DeQueue()<<endl; cout<<Q.DeQueue()<<endl; Q.DeQueue(); return 0; }
两栈共享空间的顺序栈C++实现
程序中如果需要同时使用具有相同数据类型的两个栈的时候,可以采用一个数组,让一个栈的栈底在该数组的起始端,另一个栈的栈底在数组的结尾,有利于节省空间。
/************************************************************************* > File Name: BothStack.cpp > Author: Shorey > Mail: shoreybupt@gmail.com > Created Time: 2015年03月22日 星期日 09时49分06秒 ************************************************************************/ #include<iostream> using namespace std; #define StackSize 100 class BothStack { public: BothStack(){ //构造函数 top1=-1; top2=StackSize; } ~BothStack(){;} //析构函数 void Push(int i,int x);//将x元素压入栈i int Pop(int i); //对栈i进行出栈操作 int GetTop(int i); //取栈i的栈顶 bool Empty(int i); //判断栈是否为空 private: int data[StackSize]; int top1,top2; }; void BothStack::Push(int i,int x) { if(top1==top2-1) { cout<<"stack is full!"<<endl; return ; } if(i==1) data[++top1]=x; if(i==2) data[--top2]=x; } int BothStack::Pop(int i) { if(i==1) { if(top1==-1) { cout<<"stack is empty"<<endl; } else return data[top1--]; } if(i==2) { if(top2==StackSize) { cout<<"stack is empty"<<endl; } else return data[top2++]; } } int BothStack::GetTop(int i) { if(i==1) { if(top1==-1) { cout<<"stack is empty"<<endl; } else return data[top1]; } if(i==2) { if(top2==StackSize) { cout<<"stack is empty"<<endl; } else return data[top2]; } } bool BothStack::Empty(int i) { if(i==1) { if(top1==-1)return true; else return false; } if(i==2) { if(top2==StackSize)return true; else return false; } } int main() { BothStack s; s.Push(1,4); s.Push(1,5); s.Push(2,8); s.Push(2,9); cout<<s.GetTop(1)<<endl; cout<<s.GetTop(2)<<endl; return 0; }
http://blog.csdn.net/xiaolei09bupt/article/list/2
浙公网安备 33010602011771号