Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hey !! i have read all of your comments and i understand !! i know how debugger works and all that but the problem is that i want to sort linked list, i already sorted out once in my code but this time i want to use that sorting in my next program but here it doesn't seem to work !! i am clipping both my previous and recent sorting function !! basically i want to sort the record by registration no. !! here is my stuff !!

What I have tried:

//Previous
struct Node
{

	Node * next;
	int data;
};

void Sort::insert(Node * &ptr, int d)
	{
		Node *temp = new Node(); // middle Node
		temp->next = ptr;
		ptr = temp; // 
		ptr->data = d;
	}

void sortNodes(Node * ptr)
	{

		int temp = 0;
		Node * current;
		for (bool swap = true; swap; ) {
			swap = false;
			for (current = ptr; current->next != NULL; current = current->next)
			{
				if (current->data > current->next->data)
				{
					temp = current->data;
					current->data = current->next->data;
					current->next->data = temp;
					swap = true;
				}
			}
		}
	}


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

	student() {}
	~student() {}
	struct student *next;
};
student *head = NULL;

void addRecord()
{
	cout << endl;
	cout << "Add record:" << endl;
	cout << endl;
	struct student *temp, *alt;
	temp = new student;
	cout << "What is the student's name: ";
	cin >> temp->name;
	cout << "What is the student's RegNo: ";
	cin >> temp->regno;
	cout << "What is the student's Age: ";
	cin >> temp->age;
	cout << "What is the student's PhonNo: ";
	cin >> temp->pono;
	cout << "What is student's GPA: ";
	cin >> temp->GPA;
	temp->next = NULL;
	if (head == NULL)
		head = temp;
	else
	{
		alt = head;
		while (alt->next != NULL)
		{
			alt = alt->next;
		}
		alt->next = temp;
	}
}


void sorting(void)
{
	student * temphead = head;
	student * tempnode = NULL;
	int count = 0;
	for (int i = 0; i<count; i++) {
		for (int j = 0; j<count - i; j++) {
			if (temphead->regno > temphead->next->regno) {
				tempnode = temphead;
				temphead = temphead->next;
				temphead->next = tempnode;
			}

			temphead = temphead->next;
			count++;
			
		}
	}
}


//main
sorting();

//error is it doesnt shows the error or output i tried debugger too !!
Posted
Updated 6-Jan-17 15:20pm
v2
Comments
[no name] 5-Jan-17 12:20pm    
"i know how debugger works", for some strange reason I don't believe you. I don't believe you for one simple reason. If you had done what you have been told multiple times and used the debugger to step through your code to find out what was going on with your code, you would be able to provide a proper report of a problem with your code that is not "doesn't seem to work" which means absolutely nothing to anyone but you.
nv3 5-Jan-17 14:11pm    
I totally agree. If OP new how to use a debugger he/she would not have posted this question, but found the problem.
Afzaal Ahmad Zeeshan 5-Jan-17 13:30pm    
How but how do you go on "outputting" anything?
Philippe Mori 5-Jan-17 14:45pm    
And the answer is obvious : use a debugger to see where you have made a mistake. Start with 2 or 3 items in different order.
mayashah 6-Jan-17 4:11am    
Solved !! :)

Read the article How to create Linked list using C/C++. It also handles sorting.

The problem in all sorting algorithms is that by sorting the position is changing, so you need to restart the ordering at some point.

I also recommand you to read the sorting article on the wikipedia to acquire some deeper knowledge about sorting. It will help you to present your homework with some fancy details.

Pro-tip: write some test data in code to test your sort functions.
 
Share this answer
 
Quote:
i know how debugger works
Your question tells us that you don't know how debugger works.

The debugger is a tool and like for any tool, you need to learn how to use it.

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.

Advice: take a sheet of paper and try to do the algorithm by hand, your program should use the same procedure.
On the sheet of paper, note all changes in your list after each steps of the sorting. It is a linked list, only the links to head and next must change.
 
Share this answer
 
Comments
mayashah 6-Jan-17 21:53pm    
i solved it !! created new logic debugger sucks !! too complicated for an amateur
Patrice T 6-Jan-17 22:05pm    
Even if it sucks, the debugger is still the best tool to understand why a program fails.

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