循环链表,程序总是终止。求高手!题目有的

循环链表,程序总是终止。求高手!题目有的,第1张

#include<iostream>

#ifndef QUEUE

#define QUEUE

using namespace std

typedef int QueueElement //int和QueueElement同名

class Queue

{

public:

Queue()//构造函数

Queue(const Queue &original)//复制构造函数

~Queue()//析构函数

const Queue &operator= (const Queue &rightHandSide)//赋值运算符重载

bool empty() const

void enqueue(const QueueElement &value)

QueueElement front() const

void display(ostream &out) const

void dequeue()

private:

/*** Node class ***/

class Node

{

public:

QueueElement data

Node * next

Node(QueueElement value, Node * link = 0)

{ data = valuenext = link}

}

typedef Node* NodePointer//NodePointer和Node*同名

/***** Data Members *****/

NodePointer myBack //设置一个指向节点的指针myBack

}

Queue::Queue()

{

myBack=0

}//构造函数,初始化myBack

Queue::Queue(const Queue &original)

{

myBack = 0

if (!original.empty())

{

myBack = new Queue::Node(original.myBack->data)

Queue::NodePointer origPtr = original.myBack->next

while (origPtr != original.myBack)

{

myBack->next = new Queue::Node(origPtr->data)

myBack = myBack->next

origPtr = origPtr->next

}

}

}//拷贝构造函数

Queue:: ~Queue()

{

while(!empty())

{

dequeue()

}

}//析构函数

const Queue &Queue:: operator= (const Queue &rightHandSide)//???

{

if (this != &rightHandSide) // check that not q = q

{

this->~Queue() // destroy current linked list

if (rightHandSide.empty()) // empty queue

myBack = 0

else

{// copy rightHandSide's list

// Copy first node

myBack = new Queue::Node(rightHandSide.myBack->data)

Queue::NodePointer ptmp=myBack

Queue::NodePointer rhsPtr = rightHandSide.myBack->next

while (rhsPtr != rightHandSide.myBack)

{

ptmp->next = new Queue::Node(rhsPtr->data)

ptmp = ptmp->next

rhsPtr = rhsPtr->next

}

ptmp->next=myBack

}

}

return *this

} //赋值运算符重载

bool Queue::empty() const

{

return (myBack == 0)

} //判断是否为空

void Queue::enqueue(const QueueElement &value)

{

Queue::NodePointer newptr = new Queue::Node(value)

if (empty())

{

myBack = newptr

myBack->next=myBack

}

else

{

newptr->next=myBack

Queue::NodePointer ptmp = myBack

while(ptmp!=NULL)

{

if(ptmp->next==myBack)

{

ptmp->next=newptr

break

}

else

ptmp = ptmp->next

}

}

} //增加一个元素

QueueElement Queue:: front() const

{

if (!empty())

return (myBack->data)

else

{

cerr<<"Queue is empty: error! Returning garbage value\n"

QueueElement garbage

return garbage

}

}

void Queue:: display(ostream &out) const

{

if (empty()) return

else

{

for (Queue::NodePointer ptr = myBackptr = ptr->next)

{

out <<ptr->data <<" "

if(ptr->next==myBack)

{

break

}

}

}

out <<endl

}//输出链表

void Queue::dequeue()

{

if (myBack == 0)

cerr<<"Queue is empty\n"

else

{

Queue::NodePointer ptr = myBack,ptmp

if (ptr->next == ptr)

{

delete ptr

myBack = 0

} //只有一个元素

else

{

myBack = myBack->next

ptmp = myBack

while(ptmp!=NULL)

{

if(ptmp->next==ptr)

{

ptmp->next=myBack

delete ptr

break

}

else

ptmp = ptmp->next

}

}

}

}

#endif

main.cpp没变动

#include <iostream>

#include "queue.h"

using namespace std

int main()

{

Queue q1

cout <<"Queue created. Empty? " <<q1.empty() <<endl

cout <<"How many elements to add to the queue? "

int numItems

cin >>numItems

for (int i = 1i <= numItemsi++)

q1.enqueue(i)

cout <<"Contents of queue q1 (via print):\n"

q1.display(cout)

cout <<endl

Queue q2

q2 = q1

cout <<"Contents of queue q2 after q2 = q1 (via print):\n"

q2.display(cout)

cout <<endl

cout <<"Queue q2 empty? " <<q2.empty() <<endl

cout <<"Front value in q2: " <<q2.front() <<endl

while (!q2.empty())

{

cout <<"Remove front -- Queue contents: "

q2.dequeue()

q2.display(cout)

}

cout <<"Queue q2 empty? " <<q2.empty() <<endl

cout <<"Front value in q2?" <<endl <<q2.front() <<endl

cout <<"Trying to remove front of q2: " <<endl

q2.dequeue()

return 0

}

gets(p1->name) //输入name,sex,num,score

p1->sex=getchar()

scanf("%d%f",&p1->num,&p1->score)

getchar()

head=NULL

while(p1->num!=0) //结束条件为num=0

{

n+=1

printf("%d\n",n+1)

if(n==1) head=p1

else p2->next=p1,p2=p1

p1=(stud*)malloc(LEN)

gets(p1->name)

p1->sex=getchar()

scanf("%d%f",&p1->num,&p1->score)

getchar()

}

只看到这里,问题已经不小了 分析如下:

1. gets(p1->name) //输入name,sex,num,score

gets函数不检查输入字符串长度,不建议使用

2. p1->sex=getchar() //这里会把回车符读进去,sex没有被赋有效值而是‘\n’

3. if(n==1) head=p1

else p2->next=p1,p2=p1

else语句最好加括号,规范化写法。

4. 排序算法问题,注意循环逻辑,刚找到第一个小的(不一定是最小),你就开始拆链表,如何不错?

总结一下: 写程序需要细心,刚开始写程序需要学会调试程序,望LZ采纳。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11748058.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存