循环链表 [英] circular linked list

查看:83
本文介绍了循环链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我

编写具有三个成员变量的struct NodeType
编写程序以使用具有四个成员变量的类TotalLinkedList的循环链表

NodeType
名称:字符串
等级:int
链接:NodeType *

您的代码将插入学生姓名和他的成绩,然后:

如果等级> = 50,则将节点插入指针Pass
的第一个 如果等级< 50,将节点插入到指针Fail的第一个
如果成绩无效,则不要插入.

TotalLinkedList
-Pass:NodeType *
-失败:NodeType *
-CountPass:int
-CountFail:int
+ insert(string,int):void
+ deleteNode(int):void
+ searchName(字符串):int
+ FindMax():字符串
+ PrintAverage():double
+打印():void
+ TotalLinkedList()
+〜TotalLinkedList()
+ TotalLinkedList(TotalLinkedList&)

DeleteNode(int):获取等级并删除具有该等级的所有节点

SearchName(字符串):获取学生姓名,如果找到,则返回其成绩,否则返回-1
打印学生姓名及其成绩

在主要功能中

please help me

Write a struct NodeType with three member variables
Write a program to use a circular linked list for class TotalLinkedList with four member variables

NodeType
Name : string
Grade : int
Link : NodeType *

Your code will insert the student name and his grade then:

If the grade >= 50, insert the node at the first of pointer Pass
If the grade < 50, insert the node at the first of pointer Fail
If the grade is invalid then don’t do the insert.

TotalLinkedList
-Pass: NodeType *
-Fail: NodeType *
-CountPass : int
-CountFail: int
+ insert(string, int ) : void
+ deleteNode (int) : void
+ searchName (string) : int
+ FindMax( ): string
+PrintAverage(): double
+ print ( ) : void
+ TotalLinkedList( )
+ ~TotalLinkedList( )
+ TotalLinkedList (TotalLinkedList & )

DeleteNode(int): takes the grade and delete all the node with this grade

SearchName( string): takes the student name and return his grade if found else return -1
Print the students name and their grades

In the main function

TotalLinkedList   Obj1;

Obj1.insert("Ali" , 90);
Obj1.insert("Ahmed", 44);
Obj1.insert("Ruba", 103);
Obj1.insert("Majd", 77);
Obj1.insert("Huda" , 85);
Obj1.insert("Kamel", 39);
Obj1.insert("Ola", -5);
Obj1.insert("Eman", 90);

Now Obj1.deleteNode(90);



现在...



Now...

Obj1. print( );   // print the students names and their grades
  
cout<<Obj1.searchName("Ahmad");     // prints the grades of Ahmad (-1 if does not exist)
   //prints  44

cout<<printAverage( ) <<endl; //print the avergae of the students
//61.25

cout<<findMax() <<endl; //prints the student name with hieghest grade
//Huda



您需要请用户插入名称或从文件中读取文件(我更喜欢第二种选择)...
您需要要求用户删除成绩...

添加了代码块[/编辑]

enhzflep-从注释中向Paul添加了代码.



You need to ask the user to insert the names or to read from files (I prefer the second choice)...
You need to ask the user to delete a grade...

Code block added[/Edit]

enhzflep - Added code from comment to Paul.

#include <iostraem>
#include <string>
using namespace std;
struct NodeType 
{
 string Name ;
 int Grade ;
 NodeType* Link ;

}
class TotalLinkedList 
{
private:
	NodeType* Pass;
	NodeType* Fail ;
	int CountPass ;
	int CountFail ;

public :
	void insert(string , int );
	void deletNode(int);
	int searchName(string) ;
	int FindMax (string);
	double PrintAverage ();
	void Print ();
	TotalLinkedList();
	~TotalLinkedList();
	TotalLinkedList(TotalLinkedList&);
}
TotalLinkedList::insert (string name ,int grade )
{
	
	cin >> grade ;
	cin >> name ;
	
	if ( grade >=50 )
	{
	   Pass->Name =name ;
	   Pass->Grade =grade
	}
	else 
	{
	   Fail->Name=name; 
	   Fail->Grade=grade;
	}
	else 
	{
	   cout << " can not insert " << endl;
	}  // closing brace missing from source provided by op
}  // closing brace missing from source provided by op

int main ()
{
   return 0 ;
}

推荐答案

循环喜欢的列表与通常链接的列表完全相同.唯一的不同是最后一个列表指向第一个列表,而不是NULL.看一下这个简单的代码:

A circular liked list is exactly the same as the usually linked list. The only different is that the last list points to the first one, not to NULL. Have a look at this simple code:

#include <iostream>
using namespace std;

class LinkedList {
public:
    LinkedList(int value) {
        this->value = value;
    }
    int value;
    LinkedList *next;

};

int main(int argc, char **argv)
{
    LinkedList *root       = new LinkedList(1);
    root->next             = new LinkedList(2);
    root->next->next       = new LinkedList(3);
    root->next->next->next = new LinkedList(4);

    root->next->next->next->next = root; // the last list is linked with the fist one

    cout << root->value << endl;                         // 1
    cout << root->next->value << endl;                   // 2
    cout << root->next->next->value << endl;             // 3
    cout << root->next->next->next->value << endl;       // 4 
    cout << root->next->next->next->next->value << endl; // 1 we are back!
    return 0;
}



这是魔术:



Here is the magic:

root->next->next->next->next = root; // the last list is linked with the first one



我们只需将最后一个列表与root链接即可.现在看起来像这样:



We simply linked the last list with root. Now it will look like something like this:

+-->               ---+
| +-- 1 ------- 2 --+ |
  |                 | V
  |                 |
  |                 |
^ |                 |
| +-- 4 ------- 3 --+ |
+---               <--+


我们不做您的作业:它的设置是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

自己尝试,您可能会发现它并不像您想的那么困难!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


这篇关于循环链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆