大大们,帮忙做一下队列在链式存储结构

aij123 2006-12-05 10:19:28
队列在链式存储结构的建立、插入、删除、查找等运算。这要怎么做啊
...全文
279 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wellsnow2002 2006-12-05
  • 打赏
  • 举报
回复
楼上的,你用类模板,他看 部懂啊,他应该刚刚才开始学数据结构啊!晕!
DonaldKnuth 2006-12-05
  • 打赏
  • 举报
回复
//对链式队列的测试
#include "linkedQueue.h"
#include <iostream>
using namespace std;

void main()
{
try
{
int c;
cout<<"please input the number:"<<endl;
LinkedQueue <int> q;
for(int i=1;i<=9;i++)
q.Add(i);

for(i=1;i<=9;i++)
{
q.Delete(c);
cout<<c<<" "<<endl;
}


}

catch (const exception & e)
{
cerr << "exception:" <<e.what()<<endl;
}
}
DonaldKnuth 2006-12-05
  • 打赏
  • 举报
回复
/*=============================================================
*********************使用链表形成的队列************************
=============================================================*/
#ifndef _LINKEDQUEUE_H
#define _LINKEDQUEUE_H

#include <exception>

template<class T>
class LinkedQueue;
//---------------------------------------------------------
template <typename T>
class Node
{
friend LinkedQueue<T>; //使用前LinkedQueue必须先声明
private:
T data;
Node<T> *link;
};

//---------------------------------------------------------

template<class T>
class LinkedQueue
{
// FIFO对象
public:
LinkedQueue() {front = rear = 0;} // 构造函数
~LinkedQueue(); // 析构函数
bool IsEmpty() const
{return ((front) ? false : true);}
bool IsFull() const;
T First() const; // 返回第一个元素
T Last() const; // 返回最后一个元素
LinkedQueue<T>& Add(const T& x);
LinkedQueue<T>& Delete(T& x);

//自定义的异常处理类
public:
class NoMem:public std::exception
{
public:
virtual const char* what() const throw()
{
return "there is no memory!";
}
};

class OutOfBounds:public std::exception
{
public:
virtual const char* what() const throw()
{
return "out of bound!";
}
};

private:
Node<T> *front; // 指向第一个节点
Node<T> *rear; //指向最后一个节点
};
//-----------------------------------------------------------

template<class T>
LinkedQueue<T>:: ~LinkedQueue( )
{ // 队列析构函数,删除所有节点
Node<T> *next;
while(front)
{
next=front->link;
delete front;
front=next;
}
}
//------------------------------------------------------------

template<class T>
bool LinkedQueue<T>::IsFull() const
{ // 判断队列是否已满
Node<T> *p;
try
{
p=new Node<T>;
delete p;
return false;
}
catch (NoMem)
{return true;}
}
//--------------------------------------------------------------

template<class T>
T LinkedQueue<T>::First() const
{ // 返回队列的第一个元素
// 如果队列为空,则引发异常OutOfBounds
if(IsEmpty())
throw OutOfBounds();
return front->data;
}
//-------------------------------------------------------------

template<class T>
T LinkedQueue<T>::Last() const
{ // 返回队列的最后一个元素
// 如果队列为空,则引发异常OutOfBounds
if(IsEmpty())
throw OutOfBounds();
return rear->data;
}
//-------------------------------------------------------------

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x)
{ // 把x 添加到队列的尾部
// 不捕获可能由n e w引发的NoMem 异常
// 为新元素创建链表节点
Node<T> *p = new Node<T>;
p->data=x;
p->link=0;
// 在队列尾部添加新节点
if(front)
rear->link=p; //队列不为空
else
front=p; // 队列为空
rear=p;
return *this;
}
//-------------------------------------------------------------

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{
// 删除第一个元素,并将其放入x
// 如果队列为空,则引发异常OutOfBounds
if(IsEmpty())
throw OutOfBounds();
//保存第一个节点中的元素
x=front->data;
// 删除第一个节点
Node<T> *p=front;
front=front->link;
delete p;
return *this;
}
//------------------------------------------------------------


#endif
aij123 2006-12-05
  • 打赏
  • 举报
回复
给一下程序好吗
aij123 2006-12-05
  • 打赏
  • 举报
回复
谢了。
Alan_lz 2006-12-05
  • 打赏
  • 举报
回复
// 怎么编译都不通过。
// 我改了改。在MSVC6下通过。

// 还有,你的拼音太搞(笑)了。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
typedef int ElemType;

typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode;
typedef struct
{
QNode *front, *rear;
}L_Queue;

L_Queue Q1;
void init_Q(L_Queue *Q);
void out_Q(L_Queue *Q);
void EnQueue(L_Queue *Q,ElemType e);
ElemType DeQueue(L_Queue *Q);
void find(L_Queue *Q, ElemType y);

int main()
{
int k,y;
ElemType e,x;
char ch;
init_Q( &Q1);
do
{
printf("\n\n\n");
printf("\n\n 1. shuju yuansu e jin duilei ");
printf("\n\n 2. shudui yige yuansu,fanhui qi zhi");
printf("\n\n 3. qing shuru yige yuansu jinxing chazhao ");
printf("\n\n 4. jieshu chengxu yunxing ");
printf("\n======================================");
printf("\n qing shuru ni de xuanze (1,2,3,4)");
scanf("%d",&k);
switch(k)
{
case 1:
{
printf("\n jindui yuansu e=?");
scanf("%d",&e);
EnQueue(&Q1,e);
out_Q(&Q1);
} break;

case 2:
{
x=DeQueue(&Q1);
printf("\nchudui yuansu wei : %d", x);
out_Q(&Q1);
} break;

case 3:
{
printf("yao chazhao de shu :");
scanf("%d",&y);
find(&Q1,y);
}break;
case 4: printf("\n beybey !");
}

printf("\n ----------------");
}
while(k>=1 && k<3);
printf("\n da huichejian,tuichu.");
ch=getch();

return 0;
}


void init_Q(L_Queue *Q)
{
QNode *p ;
p=(QNode *)malloc(sizeof(QNode));
p->next=NULL;
Q->front=p;
Q->rear=p;
}

void out_Q(L_Queue *Q)
{
QNode *p;
char ch;
p=Q->front->next;
printf("\n dangqian duilei zhong de yuansu shi:");
while(p!=NULL)
{
printf("\n %d",p->data);
p=p->next;
}
printf("\n da huichejian,jixu. ");
ch=getch();
}

void EnQueue(L_Queue *Q,ElemType e)
{
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
Q->rear->next=s;
Q->rear=s;
}


ElemType DeQueue(L_Queue *Q)
{
ElemType x; QNode *p;
if(Q->front==Q->rear)
{
printf("\n Queue is NULL!");
x=-1;
}
else
{
p=Q->front->next;
x=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
}
return(x);
}
void find(L_Queue *Q, ElemType y)
{
int chazhao=0;
QNode *p;
p=Q->front;

while (p!=NULL)
{

if(y==p->data)
{
chazhao=1;
break;
}
else p=p->next;
}
if (chazhao>=1) printf("cha dao gai zhi\n");
else printf("cha bu dao gai zhi\n");
}
aij123 2006-12-05
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
typedef int ElemType;

typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode;
typedef struct
{
QNode *front, *rear;
}L_Queue;

L_Queue Q1;
void init_Q(L_Queue *Q);
void out_Q(L_Queue *Q);
void EnQueue(L_Queue *Q,ElemType e);
ElemType DeQueue(L_Queue *Q);
main()
{
int k,y;
ElemType e,x;
char ch;
init_Q( &Q1);
do
{
printf("\n\n\n");
printf("\n\n 1. shuju yuansu e jin duilei ");
printf("\n\n 2. shudui yige yuansu,fanhui qi zhi");
printf("\n\n 3. qing shuru yige yuansu jinxing chazhao ");
printf("\n\n 4. jieshu chengxu yunxing ");
printf("\n======================================");
printf("\n qing shuru ni de xuanze (1,2,3,4)");
scanf("%d",&k);
switch(k)
{
case 1:
{
printf("\n jindui yuansu e=?");
scanf("%d",&e);
EnQueue(&Q1,e);
out_Q(&Q1);
} break;

case 2:
{
x=DeQueue(&Q1);
printf("\nchudui yuansu wei : %d", x);
out_Q(&Q1);
} break;

case 3:
{
printf("yao chazhao de shu :");
scanf("%d",&y);
find(&Q1,y);
}break;
case 4: printf("\n beybey !");
}

printf("\n ----------------");
}
while(k>=1 && k<3);
printf("\n da huichejian,tuichu.");
ch=getch();
}


void init_Q(L_Queue *Q)
{
QNode *p ;
p=(QNode *)malloc(sizeof(QNode));
p->next=NULL;
Q->front=p;
Q->rear=p;
}

void out_Q(L_Queue *Q)
{
QNode *p;
char ch;
p=Q->front->next;
printf("\n dangqian duilei zhong de yuansu shi:");
while(p!=NULL)
{
printf("\n %d",p->data);
p=p->next;
}
printf("\n da huichejian,jixu. ");
ch=getch();
}

void EnQueue(L_Queue *Q,ElemType e)
{
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
Q->rear->next=s;
Q->rear=s;
}


ElemType DeQueue(L_Queue *Q)
{
ElemType x; QNode *p;
if(Q->front==Q->rear)
{
printf("\n Queue is NULL!");
x=-1;
}
else
{
p=Q->front->next;
x=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
}
return(x);
}
void find(L_Queue *Q,ElemType y)
{
int chazhao=0;
QNode *p;
p=Q;

while (p!=NULL)
{

if(y==p->data)
{
chazhao=1;
break;
}
else p=p->next;
}
if (chazhao>=1) printf("cha dao gai zhi\n");
else printf("cha bu dao gai zhi\n");
}
看看我的查找的问题在那,我找不到
listart 2006-12-05
  • 打赏
  • 举报
回复
队列先进先出的,还要查找?
同时二分查找一个先天条件就是:排好序了
链结构不适合做2分查找的(你可以尝试下找出一个链中间的那个环,如果你有不遍历链表就查找到那个环----前提:不加上其他辅助----你将改变世界)
wellsnow2002 2006-12-05
  • 打赏
  • 举报
回复
想帮你,我自己也在复习数据结构,好多东西都忘记了,爱莫能助!
aij123 2006-12-05
  • 打赏
  • 举报
回复
我真的看不懂,最好是用makenull(q),empty(q),enqueue(q),dequeue(q),getqueue(q),current-size(q)等函数写,查找用二分查找法写,谢谢了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧