3 回答
![?](http://img1.sycdn.imooc.com/54584cfb0001308402200220-100-100.jpg)
TA贡献1884条经验 获得超4个赞
这是我前几天刚写的,可能对你有帮助!(一个工程,自己看着操作吧!)
文件名h:
template <class T> struct node
{ T data;
node *next;
};
template <class T> class linked_Queue //链队列类
{ private: //数据成员
node<T> *front; //链队列队首指针
node<T> *rear; //链队列队尾指针
int n; //链队列长度为n
public: //成员函数
linked_Queue(); //构造函数,建立空队列,即队列初始化
void prt_linked_Queue(); //顺序输出链队列中的元素
int flag_linked_Queue(); //检测链队列的状态
void ins_linked_Queue(T); //入队
void del_linked_Queue(); //出队,并打印输出出队元素值
};
文件名.cpp
#include <iostream.h>
#include "zah.h"
template <class T> linked_Queue<T>::linked_Queue()
{
front = NULL;
rear = NULL;
n = 0;
}
template <class T> void linked_Queue<T>::prt_linked_Queue()
{
node<T> *p;
p=front;
while(p!=0)
{
cout<<p->data<<endl;
p=p->next;
}
cout<<endl;
}
template <class T> int linked_Queue<T>::flag_linked_Queue()
{
if(front==NULL)
return 0;
return 1;
}
template <class T> void linked_Queue<T>::ins_linked_Queue(T x) //入队
{
node<T> *p;
p=new node<T>;
p->data=x;
rear->next=p;
n++;
}
template <class T> void linked_Queue<T>::del_linked_Queue() //出队
{
if(flag_linked_Queue()==0)
cout<<"队列为空,无法输出!"<<endl;
node<T> *p;
p=front;
front=front->next;
cout<<p->data<<"出队"<<endl;
delete p;
n--;
}
void main()
{
linked_Queue<int> queue;
int x[5] = {1,2,3,4,5};
for(int i=0; i<5; i++)
queue.ins_linked_Queue(x[i]);
queue.prt_linked_Queue();
queue.del_linked_Queue();
queue.prt_linked_Queue();
queue.del_linked_Queue();
queue.del_linked_Queue();
queue.del_linked_Queue();
queue.prt_linked_Queue();
}
![?](http://img1.sycdn.imooc.com/5458453d0001cd0102200220-100-100.jpg)
TA贡献1770条经验 获得超3个赞
示例如下:
#include<iostream>
using namespace std;
//线性表的单链表存储表示
struct LNode
{
int data;
struct LNode *next;
};
//逆序创建链表
void CreateList(LNode *L,int a[],int n)
{
LNode *s;
L->next = NULL;
for(int i=n-1;i>=0;--i)
{
s= new LNode;
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
//显示链表
void display(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
//插入结点操作
void ListInsert(LNode *L,int d, LNode *s)
{
LNode *p;
int i=1;
p=L->next;
while(i<d-1)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
}
//删除节点操作
void ListDelete(LNode *L,int d,int &e)
{
LNode *p,*q;
int i=0;
p=L->next ;
while(i<d-1)
{
q=p;
p=p->next;
i++;
}
e=p->data;
q->next=p->next;
delete p;
}
//查找元素
LNode* LocalElem(LNode *L,int e)
{
LNode *p;
p=L->next;
while(p&&p->data!=e) p=p->next;
return p;
}
//逆置单链表
void InvertLinkedList(LNode *L)
{
LNode *p,*s;
p=L->next;
L->next=NULL;
while(p)
{
s=p;
p=p->next;
s->next=L->next;
L->next=s;
}
}
int main()
{
LNode *H;
int n;
cout<<"please enter the length of the list: ";
cin>>n;
int *A=new int[n];
int i;
H=new LNode;
H->next=NULL;
cout<<"please enter "<<n<<" number of the list:"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
CreateList(H,A,n);
cout<<"show the members of the list: ";
display(H);
cout<<endl;
int d;
cout<<"please enter the address of the add member: ";
cin>>d;
LNode *s;
s= new LNode;//初始化局部变量s
cout<<"please enter the data of the add member: ";
cin>>s->data;
ListInsert(H,d, s);
display(H);
cout<<endl;
int p;
cout<<"please enter the address of the delete member: ";
cin>>p;
int c=0;
int &e=c;//非常量引用的初始值必须为左值!!!
ListDelete(H,p,e);
display(H);
cout<<endl;
int g;
cout<<"please enter the member that you want to find: ";
cin>>g;
cout<<"the local of the member is: "<<LocalElem(H,g);
cout<<endl;
InvertLinkedList(H);
cout<<"the invertlinklist is: ";
display(H);
cout<<endl;
return 0;
}
- 3 回答
- 0 关注
- 126 浏览
添加回答
举报