GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-May-2004, 08:05
moltarim moltarim is offline
New Member
 
Join Date: May 2004
Posts: 3
moltarim is on a distinguished road
Unhappy

linked lists, newbie needs help


required to write a c prog to display sine waves. the user inputs values for frequency, amplitude and phase. the values are stored in a linked list structure e.g

CPP / C++ / C Code:
typedef struct wave_info
{
     double freq;
     double amp;
     double phase;
     struct wave_info *next;
}WAVE;

user must be able to add waves, delete waves, edit the characteristics of a selected wave. the current wave and summation of all waves entered must be able to be displayed in graphics window when called upon,and must represent the contents of the linked list at all times.....

help needed, this is a bit complicated for a newbie like myself!! any code or hints/pointers would be greatly appreciated........ i have attached what code i have assembled so far but help would be much appreciated..
Attached Files
File Type: doc waves.doc (29.5 KB, 53 views)
Last edited by dsmith : 06-May-2004 at 08:15. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 06-May-2004, 08:55
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Moltarim. Welcome to GIDForums

First of all a quick question. Are you using word for an editor? I am not sure what advantages that is giving you, but the disadvantages are several, no highlighting and you need to convert it to a text file to compile. Just a question.

Second. You should probably start by making sure you know exactly how to add and delete data from a linked list and make sure that it is working properly. It appears that you have copied code from a car search. It appears to be correct, but if you don't understand it 100% it may be more trouble to morph your program into this format.

I would start out with a few simple functions just for testing purposes. This is a very simplified start to linking data.
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct wave_info
{
     double freq;
     double amp;
     double phase;
     struct wave_info *next;
}WAVE;

/*GLOBALS to track current and head position always*/
WAVE* 	current;
WAVE*	head;


void add_wave(WAVE* new_wave)
{

	new_wave->next = 0;
	if(head){			/*There is data in the list already*/
		if(!current)				/*Just in case current is off the list*/
			current = head;
		while(current->next)		/*Go to last position in list*/
			current= current->next;
		current->next = new_wave;   /*Set new_wave at end*/
    }
	else
		head = new_wave;			/*If no data put at head position*/

}

void del_wave()
{
	WAVE* temp;

	if(current){					/*Make sure we are pointing at something*/
		if(head = current)			/*If at the front of the list just reassing head*/
			head = head->next;
		else{
			temp = head;
			while(temp->next != current)	/*Place temp right before current*/
				temp = temp->next;
			temp->next = current->next;		/*Loop around current*/
		}
		free(current); 						/*Free memory*/
		current = head;
	}
}


void list_all()
{
	if(head){				/*Check to see if there is data*/
		current = head;		/*Goto top of list*/
		do{
			printf("Freq: %f - Amp: %f - Phase: %f\n",current->freq,current->amp,current->phase);
			current = current->next;		/*Position current to next data*/
		}while(current);	/*Loop until current points to null*/
    }

}

void list_current()
{
	if(current)
		printf("Freq: %f - Amp: %f - Phase: %f\n",current->freq,current->amp,current->phase);
}


int main()
{
	char	string[50];
	char    select;
	WAVE*	new_wave;

	head = current = 0;

	do{
		printf("Select a function: \n");
		printf("\tA - Add a wave.\n");
		printf("\tD - Delete a wave.\n");
		printf("\tH - Go to first wave.\n");
		printf("\tN - Go to next wave.\n");
		printf("\tP - Print waves.\n");
		printf("\tX - Exit Program.\n");
		printf("\nEnter Selection: ");
		fgets(string,4,stdin);
		select = toupper(string[0]);
		switch(select){
			case 'A':
				new_wave = (WAVE*) malloc(sizeof(WAVE));
                printf("Enter Frequency: ");
				fgets(string,49,stdin);
				new_wave->freq = atof(string);
				printf("Enter amplitude: ");
				fgets(string,49,stdin);
				new_wave->amp = atof(string);
				printf("Enter phase: ");
				fgets(string,49,stdin);
				new_wave->phase = atof(string);
				add_wave(new_wave);
				break;
			case 'D':
				del_wave();
				break;
			case 'H':
				current = head;
				if(current){
					printf("Current Wave is:\n");
					list_current();
				}
				break;
			case 'N':
				if(current){
					if(current->next)
						current = current->next;
					printf("Current Wave is:\n");
				}
				list_current();
				break;
			case 'P':
				list_all();
				break;
			case 'X':
				break;
			default:
				printf("\n\nERROR - Unknown command.\n\n");
				break;
		}
	}while(select != 'X');

	return(0);
}

I had most of the linked list stuff already implemented from other functions. I would do some testing with this and then add the stuff that you need as you are sure this works.

Good luck,
d
Last edited by dsmith : 06-May-2004 at 09:20. Reason: Added a little tighter error checking
  #3  
Old 06-May-2004, 09:59
moltarim moltarim is offline
New Member
 
Join Date: May 2004
Posts: 3
moltarim is on a distinguished road
thanx very much man, ur help was greatly appreciated...... i'm using microsoft visual C++ by the way i just pasted the code into word so i could attach it. i'll go have some fun with ur code now. i could well be back for more advice though lol.
  #4  
Old 06-May-2004, 10:07
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by moltarim
thanx very much man, ur help was greatly appreciated...... i'm using microsoft visual C++ by the way i just pasted the code into word so i could attach it. i'll go have some fun with ur code now. i could well be back for more advice though lol.

To attach a source file, either change the extention to .txt or zip it. It results in a much smaller file and is easier to manage.

Also, make sure that you run through that code that I scrawled together. In just looking at it I noticed a major f' up:

I am always deleting the first wave because of the assignment (=) instead of comparision operator (==). This
CPP / C++ / C Code:
void del_wave()
{
  WAVE* temp;

  if(current){          /*Make sure we are pointing at something*/
    if(head = current)      /*If at the front of the list just reassing head*/
      head = head->next;

Should be changed to this:
CPP / C++ / C Code:
void del_wave()
{
  WAVE* temp;

  if(current){          /*Make sure we are pointing at something*/
    if(head == current)      /*If at the front of the list just reassing head*/
      head = head->next;

Sorry. Anyway, I gave you the basics. You need to experiment with this and then add on the icing .
  #5  
Old 06-May-2004, 11:32
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by dsmith
I am always deleting the first wave because of the assignment (=) instead of comparision operator (==).

I hate to whip a dead-horse, but I also don't like to post buggy code. This error actually will cause head drift and will abandon data. It sets the head to be at the current position every time. So every data entry prior to it is lost! Moltarim, I hope that you are reading this still .

To quote one of the better movies of all time:

"It is always the little things that get me"
- Office Space, Michael Bolten.
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 CPP / C++ Forum 1 06-Mar-2008 20:33
Linked lists vortz83 CPP / C++ Forum 3 20-Mar-2004 06:56
newbie using Sam's series Skampy CPP / C++ Forum 3 06-Mar-2004 18:51
Problem with my Geforce video card Shivs Computer Hardware Forum 2 30-Jan-2004 20:54
Newbie needs help with "Permission denied" charles Apache Web Server Forum 0 30-Jan-2004 15:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.