help on linked lists any1?????
hi i am a bit of an ammature and would appreciate any help on this. i am tryin 2 write a program that displays the graph of the current wave in the linked list and the sum of all the waves in the list - it is this that does not work. viewing the current wave works fine. i have a feeling it is something simple that is wrong to do with pointers??? here is the code-
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <windows.h>
#include <math.h>
#include "n:/course/elec/include/gfxlib3.h"
typedef struct wave_type // declare structure for WAVE database
{
int number;
unsigned long amplitude;
unsigned long phase;
unsigned long frequency;
unsigned long y_val[550];
struct wave_type *next;
struct wave_type *former;
struct wave_type *previous;
struct wave_type *current;
} WAVE;
void print_wave(WAVE *item) // function to print current WAVE details
{ // to screen
int i;
if (item==NULL)
{
gotoxy(15,1); printf(" End of list ");
Sleep(100);
}
else
{
if (item != NULL,i=1,i++)
{
gotoxy(15,5); printf(" Waves in list ");
gotoxy(15,9); printf("Wave number: ");
gotoxy(15,11); printf("Amplitude: ");
gotoxy(15,13); printf("Phase(deg): ");
gotoxy(15,15); printf("Frequency(Hz): ");
gotoxy(34,9); printf("%d", item->number);
gotoxy(34,11); printf("%ld", item->amplitude);
gotoxy(34,13); printf("%ld", item->phase);
gotoxy(34,15); printf("%ld", item->frequency);
}
}
}
WAVE * add_wave(WAVE *item) // add a WAVE to the end of the linked list
{
WAVE *new_wave;
double w, freq, phase, y_val[550];
int t, amp, phas;
printf(" Adding a wave ");
new_wave = (WAVE *) malloc(sizeof(WAVE));
new_wave->next = NULL;
new_wave->previous = item;
gotoxy(15,9); printf("Wave Number: ");
gotoxy(15,11); printf("Amplitude: ");
gotoxy(15,13); printf("Phase(deg): ");
gotoxy(15,15); printf("Frequency(Hz): ");
gotoxy(34,9); scanf("%ld", &new_wave-> number);
gotoxy(34,11); scanf("%ld", &new_wave-> amplitude);
gotoxy(34,13); scanf("%ld", &new_wave-> phase);
gotoxy(34,15); scanf("%ld", &new_wave-> frequency);
freq = new_wave->frequency;
phase = new_wave->phase;
amp = new_wave->amplitude;
w = 2 * 3.142 * freq;
for (t=1; t < 550; t++)
{
new_wave->y_val[t] = amp * sin(w * t + phase);
}
if (item != NULL)
{
item->next = new_wave;
}
return (new_wave);
}
WAVE * browse(WAVE *item) // move to next item in list
{
if (item != NULL)
{
if (item->next != NULL)
{
item = item->next;
}
}
else{
gotoxy(5,5);
printf("This is the last item in the list");
Sleep(1000);
}
return (item);
}
WAVE * browseback(WAVE *item) // move to previous item in list
{
if (item != NULL)
{
if (item->previous != NULL)
{
item = item->previous;
}
}
else{
gotoxy(5,5);
printf("This is the first item in the list");
Sleep(1000);
}
return (item);
}
WAVE * delete_wave(WAVE *item,WAVE **first,WAVE **last ) // delete item from list
{
WAVE *next, *previous;
if(item!=NULL)
{
next=item->next;
previous=item->previous;
if(previous==NULL)
{
*first=next;
}
else
{
previous->next=next;
}
if(next==NULL)
{
*last=previous;
}
else
{
next->previous=previous;
}
gotoxy(8,5);
printf("This current wave has been deleted from the list");
Sleep(1000);
gotoxy(8,5);
printf(" ");
}
return (item);
}
WAVE * graphic(WAVE *item, WAVE **first, WAVE **last)
{
double w, freq, amp, phase, y_valu[550], y[550];
int i, t, j, y_value[550], y_v[550];
WAVE *next, *previous, *current;
freq = item->frequency;
phase = item->phase;
amp = item->amplitude;
w = 2 * 3.142 * freq;
for (t=1; t < 550; t++)
{
y_valu[t] = amp * sin(w * t + phase);
}
gotoxy(15,16);
graphics_on();
setfillstyle(1, WHITE); //make background white
floodfill(0, 0, WHITE);
setcolor(0);
moveto(50,50); //Draw axis
lineto(50,450);
moveto(50,250);
lineto(600,250);
setcolor(4);
moveto(50,250);
for(j=1;j<550;j++)
{
y_value[j]=floor(y_valu[j]); //round values down so they can be plotted on the graph
lineto(j+49,250-y_value[j]); //draw the current wave in the list
}
do
{
y[1]=0;
setcolor(1);
moveto(50,250);
for(i=1;i<550;i++)
{
item=first; // I THINK IT IS HERE WHERE IT GOES WRONG
do
{ y[i] = y[i] + item->y_val[i];
item = item->next;
}
while(item!=NULL);
y_v[i] = floor(y[i]);
lineto(i+49,250-y_v[i]);
}
}
while (!(kbhit()));
graphics_off();
return (item);
}
int main (void) // main function / menu
{
WAVE *first=NULL, *current=NULL, *last=NULL, *previous=NULL;
char response;
system("cls");
do
{
print_wave(current); // print current item in list to screen
gotoxy(15,19); printf("'a': add a wave");
gotoxy(15,25); printf("'v': view graphical output");
gotoxy(15,20); printf("'d': delete current wave");
gotoxy(15,21); printf("'w': brouwse the list forwards");
gotoxy(15,23); printf("'r': return to start of list");
gotoxy(15,24); printf("'q': quit the list");
gotoxy(15,22); printf("'e': brouwse backwards in the list");
gotoxy(40,29); putch(' '); // overwrite old command
gotoxy(25,29);
printf("Enter command: "); // get new command
response = getch();
gotoxy(34,40); putch(response); // print new command to screen
switch (response) // call relevent function
{
case 'd': current = delete_wave(current, &first, &last); break;
case 'a': last = add_wave(last); break;
case 'w': current = browse(current); break;
case 'v': current = graphic(current, &first, &last); break;
case 'r': current = first; break;
case 'e': current = browseback(current); break;
}
if (first == NULL) current = first = last;
} while (response != 'q');
system("cls");
return 0;
}

Last edited by dsmith : 17-May-2004 at 09:05.
Reason: Please use [c] & [/c] for syntax highlighting
|