Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am making small code for student record like thing and i want to delete a record by the regNo. but when i implement the function it stuck and program shows error report !! where i am lacking !! also tell me, is sorting and traversing same thing ?

What I have tried:

struct student
{
	string name;
	int regno;
	int age;
	double GPA;
	int pono;

	student() {}
	~student() {}
	struct student *next;
};

void deleteRecord()
{
	cout << endl;
	cout << "Delete record:" << endl;
	cout << endl;
	student *del;
	del = head;
	del = del->next;
	if (del->next == NULL)
		cout << "End of the list." << endl;
	else
		del = del->next;
	if (del == head)
		cout << "Beginning of list." << endl;
	else
	{
		student *prev;
		prev = head;
		while (prev->next != del)
		{
			prev = prev->next;
		}
		del = prev;
	}
	cout << endl;
	cout << "RegNo to delete: ";
	cin >> del->regno;

	if (del->next == NULL)
		cout << "Nothing follows." << endl;
	else
	{
		student *temp;
		temp = del->next;
		del->next = temp->next;
		delete temp;
	}
}

C++
int main()
{
deleterecord()}
Posted
Updated 4-Jan-17 22:32pm
Comments
[no name] 4-Jan-17 13:24pm    
Are we supposed to guess what "error report" you see on your screen that we can't see at all?
mayashah 4-Jan-17 20:23pm    
program stop working !!
mayashah 4-Jan-17 20:24pm    
plus the delete function seems not to work !! any guesses about function !! i just want to delete a record by regno.
[no name] 5-Jan-17 8:37am    
Well having researched your history, I would have to say that best thing you can do at this point is to drop the class and find something else to do. You don't listen to what people tell you, you refuse to perform the most basic fundamental part of the task (learning to use the debugger to debug your code) and you spend most of your time trying to get others to do your homework assignment for you (being a help vampire). Not everyone has the skill set for programming and there is no shame in finding that you are just not suited for the task.
mayashah 5-Jan-17 11:12am    
no man at least i have tried !! if i dont try then you can taunt me !!

Starting from the end of your question:
Quote:
is sorting and traversing same thing ?
No.
Sorting is the process of ordering nodes so they are in a particular sequence. If you have four nodes:
4 7 3 1
Then sorted they would become:
1 3 4 7
Or
7 4 3 1
If sorted in descending order.
Traversing is the simpler process of "walking through" each node from the head to the tail visiting each node in turn but not changing the content or position of any.

Going back to your "main" problem: We don't know what error you are experiencing, but fixing it is actually part of your assignment.
So use the debugger and follow through what your code is doing.
This is part of your homework, and getting the code to work correctly is part of the task: indeed, it's often the bit that is most interesting and time consuming!

So put a breakpoint on the first line of the function, and step through the code working out in advance what should be happening. Compare that with what did happen and when they don't match you will start to get an idea as to why. You can use the debugger to look at variable contents, as well as stepping through your code line-by-line, and for some systems you can even edit the code while it is running!

But we don't know what system and IDE you are using, so we can't give explicit instructions. Google for "debugger" and the name of your compilation system and you should find at least basic instructions on how to use it.
 
Share this answer
 
From your code, the list is already empty.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
If you want to delete the record with a specific regNo you have to find that record, adjust the next member of the previous record, and delete it. But your code is not doing that.

An untested example from scratch:
C++
void deleteRecord(int regNo)
{
    student *prev = head;
    student *temp = head;
    // Find the element.
    while (temp && temp->regNo != regNo)
    {
        prev = temp;
        temp = temp->next;
    }
    // Element has been found.
    if (temp)
    {
        // Adjust head when deleting the first element
        if (temp == head)
            head = temp->next;
        // Adjust next of previous element.
        prev->next = temp->next;
        delete temp;
    }
}
 
Share this answer
 
Comments
mayashah 5-Jan-17 8:14am    
thanks man !! it is all clear now !! will tease ya some other time :) THUMBS UP
This is your 10th question on this application and you still seem to be no further ahead. Stop what you are doing and spend some time with just pen and paper, and draw the logic of a simple linked list. The list elements only need to contain the pointer to the next item and the number. Now think about how you would go from the head of the list to find a specific element, and how you would remove that element from the list. Once you have a good understanding of the logic, you can start writing the actual code.

And remember, you cannot delete items from a list that does not contain anything, so you first need to check that it has some elements.
 
Share this answer
 

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