Click here to Skip to main content
15,922,574 members
Please Sign up or sign in to vote.
1.42/5 (5 votes)
See more:
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
C++
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...
C++
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


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...

[Edit]Code block added[/Edit]

Edit: enhzflep - Added code from comment to Paul.

C++
#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 ;
}
Posted
Updated 12-May-20 19:46pm
v5

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:

C++
#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:

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


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

+-->               ---+
| +-- 1 ------- 2 --+ |
  |                 | V
  |                 |
  |                 |
^ |                 |
| +-- 4 ------- 3 --+ |
+---               <--+
 
Share this answer
 
v3
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!
 
Share this answer
 
Comments
CpeBoshra 29-Oct-12 11:08am    
I am! Did not attend this lecture and the teacher did not agree that explain me I tried to write a little bit. My code has been added to my question.
My code to perform every operation in circular-singly linked list
.
.
.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct mynode
{
	int info;
	struct mynode *link;
}Node;
Node*start=NULL;
void insertatbegin()
{
	char c;
	while(1)
	{
		fflush(stdin);
		printf("\ndo you want to continue(y:n)?\n");
		scanf("%c",&c);
		if(c=='y'||c=='Y')
		{
	int a;
	printf("enter the data\n");
	scanf("%d",&a);
	Node *newptr,*ptr;
	newptr=(Node*)malloc(sizeof(Node));
	newptr->info=a;
	if(start==NULL)
	{
		start=newptr;
		newptr->link=start;
	}
	else
	{
		Node *ptr=start;
		while(ptr->link!=start)
		ptr=ptr->link;
		newptr->link=start;
		start=newptr;
		ptr->link=start;
	}
}
else
break;
}
}
void insertatend()
{
	char c;
	while(1)
	{
		fflush(stdin);
		printf("\ndo you want to continue(y:n)?\n");
		scanf("%c",&c);
		if(c=='y'||c=='Y')
		{
	int a;
	printf("enter the data\n");
	scanf("%d",&a);
	Node *newptr,*ptr;
	newptr=(Node*)malloc(sizeof(Node));
	newptr->info=a;
	if(start==NULL)
	{
		start=newptr;
		newptr->link=start;
	}
	else
	{
		ptr=start;
		while(ptr->link!=start)
		ptr=ptr->link;
		ptr->link=newptr;
		newptr->link=start;
	}
	
}
else
break;
}
}
void insertapos()
{
	int pos,a,b,i;
	Node *newptr,*ptr,*temp;
	ptr=start;
	temp=start;
	printf("\nEnter position and value\n");
	scanf("%d%d",&pos,&a);
	newptr=(Node*)malloc(sizeof(Node));
	newptr->info=a;
	b=1;
	fflush(stdin);
	while(temp->link!=start)
	{
		temp=temp->link;
		b=b+1;
	}
		if(pos<=b)
	{
		if(pos==1)
		{
		while(ptr->link!=start)
		ptr=ptr->link;
		newptr->link=start;
		start=newptr;
		ptr->link=start;
		printf("\nData inserted\n");
		}
		else
		{
		for(i=1;i<pos-1;i++)
		{
			ptr=ptr->link;
		}
		temp=ptr->link;
		ptr->link=newptr;
		newptr->link=temp;
		printf("\nData inserted\n");
	}
}
	else
	printf("\nwrong position!!\n");
}
void deleteatbegin()
{
	fflush(stdin);
	Node *ptr,*temp=start;
	ptr=start;
	if(start==NULL)
	{
		printf("\nThere is nothing to delete\n");
	}
	else
	{
		while(temp->link!=start)
		temp=temp->link;
		ptr=ptr->link;
		temp->link=ptr;
		start=ptr;
		printf("\ndeleted from beginning\n");
		fflush(stdin);
	}
}
void deleteatend()
{
	fflush(stdin);
	Node *ptr;
	ptr=start;
	if(start==NULL)
	{
		printf("\nThere is nothing to delete\n");
	}
	else
	{
		while(ptr->link->link!=start)
		ptr=ptr->link;
		ptr->link=start;
		printf("\deleted from end\n");
	}
}
void deletepos()
	{
		int pause;
		printf("\nEnter position\n");
		scanf("%d",&pause);
		Node *temp;
		Node *ptr;
		temp=start;
		int c=1,p=1;
		while(temp->link!=start)
	{
		temp=temp->link;
		p=p+1;
	}
		if(pause<=p)
		{
			if(pause==1)
			{
				ptr=start;
				while(temp->link!=start)
		temp=temp->link;
		ptr=ptr->link;
		temp->link=ptr;
		start=ptr;
		printf("\nData deleted from specified position\n");		
			}
			else{
		temp=start;
		for(;c<pause-1;c++)
			temp=temp->link;			
		ptr=temp->link->link;
		temp->link=ptr;
		printf("\nData deleted from specified position\n");
	}
}
	else
	printf("\n Wrong choice\n");
}
	void display()
	{
		fflush(stdin);
		Node *ptr;
		ptr=start;
		printf("\n LIST \n");
		if(start==NULL)
		{
			printf("\n There is nothing to print \n");
		}
		else
		{
			while(ptr->link!=start)
			{
				printf("->%d",ptr->info);
				ptr=ptr->link;
			}
			printf("->%d",ptr->info);
			printf("->NULL\n");
		}
		
	}
	int main()
	{
		int choice;
		printf("\n**************************MAIN MENU**************************\n");
		printf("\t1. insert data at beginning\n");
		printf("\t2. insert data at end\n");
		printf("\t3. insert data at specified position\n");
		printf("\t4. delete data from beginning\n");
		printf("\t5. delete data from end\n");
		printf("\t6. delete data from specified position\n");
		printf("\t7. display the list\n");
		printf("\t0. to exit\n");
		printf("\t Enter any option:");
		scanf("%d",&choice);
		if(choice==0)
		exit(0);
		switch(choice)
		{
			case 1:
				insertatbegin();
				getch();
				main();
				break;
			case 2:
				insertatend();
				getch();
				main();
				break;
			case 3:
				insertapos();
				getch();
				main();
				break;
			case 4:
				deleteatbegin();
				getch();
				main();
				break;
			case 5:
				deleteatend();
				getch();
				main();
				break;
			case 6:
				deletepos();
				getch();
				main();
				break;
			case 7:
				display();
				getch();
				main();
				break;
			deafult:
				printf("\n Enter the correct choice\n");
				getch();
				main();
		}
		return 0;
	}
 
Share this answer
 
Comments
CHill60 13-May-20 4:52am    
Providing homework solutions does not really help anyone.
Lecturers and teachers know about this site and can easily check for solutions just copied from here.
The students who just copy solutions are not learning anything by doing so.
And uncommented code dumps are not in any way instructional
Richard Deeming 13-May-20 13:23pm    
Also, if the OP hasn't managed to hand in their homework assignment in the last eight years, they've probably failed the course by now. :)
CHill60 14-May-20 8:20am    
We can only hope!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900