6 Replies - 384 Views - Last Post: 17 January 2013 - 08:55 AM Rate Topic: -----

#1 fangsofwhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-January 13

Linked List Problem

Posted 16 January 2013 - 02:32 PM

I am not expecting a fast answer, but I have to create a car database for my data structures class using a linked list in C, and I need to sort it by year as the data is being inputted then print it out on the screen. I have been working on this for a couple weeks and can't seem to make any head way. I understand the basic concepts of a linked list, how to traverse the list, and add to the end, etc, but when I attempt to sort it I completely mess it up. I know right now the program just stops after inputting data from the second entry, and before that I had it stuck in an infinite loop printing out the same two entries. I just don't know what I am doing wrong, and need some direction. I am not asking for you to finish my work for me, but perhaps point out what I am doing wrong, or what I should look into. I know that isn't specific, but I am not sure where to turn to. I am just frustrated and at the point of desperation. Once I finally get the basic code working I am going to clean it up, and hopefully separate it into separate functions, but this is the code that I have now. I am still working on it, and haven't stopped just because I am posting here. Thank you very much ahead of time for any help, I really do appreciate it quite a bit right now...

// Project 1 Phase II.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>

using namespace std;
typedef struct node                                                
{                                                               
	int data;               // will store information
	char make[150];
	char model[150];
	char engine[150];
	int year;
	int cost;
	node *next;             // the reference to the next node
};                              

int _tmain(int argc, _TCHAR* argv[])
{
	int count = 0;
	int cars;
	char make[150];
	char model[150];
	char engine[150];
	int year;
	int cost;
	node *head = NULL;      
	node *temp;              
	node *test;
	node *prev;
	//Ask user for how many cars to be inputted into database
	printf("How many Cars do you wish to enter?\n");
	scanf("%d", &cars);
	printf("\n");
	while (count < cars) {	
		temp = (node*)malloc(sizeof(node)); //allocate space for node 
		temp->data = count;// store data(first field)
		//Ask user for Data using carCount to input in the correct part of the structure
		printf("Enter Car\n");
		printf("Make: ");
		scanf("%s", make);  
		printf("Model: ");
		scanf("%s",model);
		printf("Cost: ");
		scanf("%d", &cost);
		printf("Year: ");
		scanf("%d", &year);
		printf("Engine ( 4 cyl, 6 cyl, etc...): ");
		scanf("%s", &engine);
		printf("\n");
		strcpy(temp->model, model); 
		strcpy(temp->engine, engine); 
		strcpy(temp->make, make); 
		temp->cost=cost;
		temp->year=year;
		temp->next=NULL;  // store the address of the pointer head(second field)
		if (head == NULL){
			head=temp;
			head->next=NULL;
			test=head;
			prev=head;
		} else {	
			if (test->year > temp->year){
				test->next = NULL;
				temp->next = test;
				prev->next = temp;
				head = prev;
			}
			while (test->next != NULL){
				test=test->next;
				prev=test;
			} 
			test->next=temp;


		}
		count = count + 1;

	}
	node *temp1;
	temp1 = head;

	while( temp1!=NULL )
	{
		cout<< temp1->data<<" ";// show the data in the linked list
		cout<< temp1->make<<" ";
		cout<< temp1->model<<" ";
		cout<< temp1->engine<<" ";
		cout<< temp1->year<<" ";
		cout<< temp1->cost<<"\n ";
		temp1 = temp1->next;   // tranfer the address of 'temp->next' to 'temp'
	}


	return 0;
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Problem

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 356
  • View blog
  • Posts: 785
  • Joined: 27-June 09

Re: Linked List Problem

Posted 16 January 2013 - 04:17 PM

First, I would consider giving your variables better names so it is easy to determine what you are intending to do with each variable. For example, I don't understand what you are trying to do with "test". As for the logic, what you should be trying to do is scan the list until you find the node that is before the spot you will insert. This spot is identifiable by the currentNode's next being NULL or having a year greater than the new node's year. You should first check for the exception cases which are when the list is empty and when the new node needs to be inserted before the head.
Was This Post Helpful? 0
  • +
  • -

#3 Voltagon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-January 13

Re: Linked List Problem

Posted 16 January 2013 - 11:33 PM

Before the loop initialise head node to store the data the increment count by 1.
Then have a node named 'next' store head->next then enter the loop, while count > cars.
Capture data to next.
Then have next store next->node.
Be sure to empty it
Was This Post Helpful? 0
  • +
  • -

#4 Voltagon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-January 13

Re: Linked List Problem

Posted 16 January 2013 - 11:40 PM

View PostVoltagon, on 16 January 2013 - 11:33 PM, said:

Before the loop, initialise the head node to store the data then increment count by 1.
Then have a node named 'next' store head->next then enter the loop, while count < cars.
Capture data to next.
Then have next store next->node.
Be sure to empty it

just fixed a few typos
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Linked List Problem

Posted 16 January 2013 - 11:51 PM

You should be separating your linked list operations into seperate functions. Any sorting priority that needs to be done can be within a function itself, or as some priority condition in the add function.

If I were developing this, I might have some of the following function prototypes

void addCar(Node **head, Node *node);
void removeCar(Node **head, Node *node);
Node *searchCar(Node *head, Node *node);


This post has been edited by jjl: 16 January 2013 - 11:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Linked List Problem

Posted 17 January 2013 - 08:35 AM

Firstly, you seem really confused about scanf. Here is a manual, and here are a few questions which you need to find answers for:
1. What will scanf return when it encounters a match error? For example, consider when scanf expects to see a series of decimal digit characters, but is instead given 'a'.
2. Will the 'a' from the above example remain in stdin?
3. If the 'a' from the example does remain in stdin, what happens next time you use scanf("%d", ...)?
4. What will scanf return when it encounters EOF (eg. when you press CTRL+Z on Windows), or some other input error?
5. Let us assume you tell scanf to put two values into two variables. What should scanf return, assuming that the values were successfully put into their variables?

If you use a C compiler rather than a C++ compiler, you shouldn't have to cast the return value of malloc. If you use a C++ compiler rather than a C compiler, you should be using new instead of malloc. Decide which language you're writing in: C or C++? From there, choose a compiler specific to your language.
Was This Post Helpful? 0
  • +
  • -

#7 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Linked List Problem

Posted 17 January 2013 - 08:55 AM

Choose a book specific to your language, too. Both C and C++ can be dangerous for people to learn by "trial and error", or by "example". Our time is better put to use when clarifying concepts for people who have already read books and manuals. I prefer not to quote from books for people who are too lazy to "learn by reading". In addition, you'll get a much more comprehensive "learn by reading" from a book than you will by reading this forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1