954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Linked list help

See I created a program that accepts three values from the user to be arranged as a linked list(ascending order). After it asks a number, it displays the content of the list. The output is like this:

Enter number: 6
List value: 6
Enter number: 4
List value: 4 6
Enter value: 2
List value: 2 4 6


However, because of my limited knowledge about lists, I coded the program without using any kind of loop. My question is, is there any way I can shorten my code using a "for loop" perhaps? and Can I sort the numbers all at once without using conditional statement? Thanks!

#include<iostream>
using namespace std;

struct node{
	int data;		//data in node
	node *next;		//pointer to next node
};

int main()
{
	node *p;
	p = new node;
	cout << "Give a number: ";
	cin >> p->data;
	p->next = NULL;
	cout << "List contains: "<< p->data<<"";

	node *q;
	q = new node;
	cout << "\nGive a number: ";
	cin >> q->data;
	p->next = NULL;
	if(q->data > p->data){
		cout << "List contains: " <<p->data<<" ";
		cout << q->data;
		p->next = q;}
	else{
		cout << "List contains: " <<q->data<<" ";
		cout << p->data;
		q->next = p;}

	node *r;
	r = new node;
	cout << "\nGive a number: " ;
	cin >> r->data;
	r->next = NULL;
	if(r->data > q->data && r->data > p->data){
		cout << "List contains: ";
		cout << ""<<p->data<<" "<<q->data<<" "<<r->data;
		q->next = r;}
	else if(r->data < q->data && r->data < p->data){
		cout << "List contains: ";
		cout << ""<<r->data<<" "<<p->data<<" "<<q->data;
		r->next = p; p->next = q;}
	else{
		cout << "List contains: ";
		cout << ""<<p->data<<" "<<r->data<<" "<<q->data;
		p->next = r; r->next = q;}


	system("pause>null");
	return 0;
}
lupacarjie
Newbie Poster
11 posts since Aug 2011
Reputation Points: 11
Solved Threads: 0
 

It might simplify the program if you write two more functions: 1) a function that inserts a new node into the list in its correct position, and 2) a function that displays all the nodes in the list. Then main() can contain a simple loop

void InsertNode(node**head, node*p)
{

}

void DisplayList(node* head)
{

}

int main()
{
   node* head = NULL; // top of linked list
   node* p = NULL; // a new node
   while(true) // an infinite loop
   {
      p = new node;
      cout << "Give a number: ";
      cin >> p->data;
      p->next = NULL;
      InsertNode(&head, p); 
      DisplayList(head);
    }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hey thanks! :) but how do I insert the node into the right position? Does that mean I have to compare the input values so they would be arranged in an ascending order? That would mean i would use condition statement?

lupacarjie
Newbie Poster
11 posts since Aug 2011
Reputation Points: 11
Solved Threads: 0
 
Hey thanks! :) but how do I insert the node into the right position? Does that mean I have to compare the input values so they would be arranged in an ascending order? That would mean i would use condition statement?

you are correct. You will have to use a loop to iterate through the linked list to find the correct spot for insertion.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

On the other note, how about a bubble sort?

lupacarjie
Newbie Poster
11 posts since Aug 2011
Reputation Points: 11
Solved Threads: 0
 

That will work too, as long as you swap only the data values instead of the whole node. The next pointers has to be kept unchanged.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank You! Thank You! :)

lupacarjie
Newbie Poster
11 posts since Aug 2011
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: