Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <windows.h>
typedef struct data_type // declare structure for sine_wave database
{
double amp;
double freq;
double phase;
struct data_type *next;
struct data_type *former;
} DATA;
void print_data(DATA *item) // function to print current SINE-WAVE details
{ // to screen
if (item==NULL)
{
gotoxy(20,5);
printf(" ");
gotoxy(20,5);
printf(" END OF THE SINE WAVE LIST ");
}
else
{
gotoxy(20,5);
printf(" ");
gotoxy(20,5);
printf(" SINE WAVE DATA ");
gotoxy(25,10); printf("Amplitude: ");
gotoxy(25,11); printf("Frequency: Hz ");
gotoxy(25,12); printf("Phase: deg ");
gotoxy(40,10); printf("%9.2lf", item->amp);
gotoxy(40,11); printf("%9.2lf", item->freq);
gotoxy(40,12); printf("%9.2lf", item->phase);
}
}
DATA * add_data(DATA *item) // add a SINE_WAVE to the end of the linked list
{
DATA *new_data;
gotoxy(20,5);
printf(" Adding a new wave ");
new_data = (DATA *) malloc(sizeof(DATA));
new_data->next = NULL;
gotoxy(25,10); printf("Amplitude: ");
gotoxy(25,11); printf("Frequency: Hz ");
gotoxy(25,12); printf("Phase: deg ");
gotoxy(40,10); scanf("%lf", &new_data->amp);
gotoxy(40,11); scanf("%lf", &new_data->freq);
gotoxy(40,12); scanf("%lf", &new_data->phase);
if (item != NULL)
{
item->next = new_data;
}
return (new_data);
}
DATA * del_data(DATA *item) // delete data from list
{
DATA *previous, *following;
if (item!=NULL) //ensure item exists to delete
{
previous=item->former; //assign former and next
following=item->next; //linksto pointers accordingly
if (previous!=NULL) previous->next=following; //reroute around
if (following!=NULL) following->former=previous;
printf("%d\n",item);
free(item); //release the item's memory
if (previous!=NULL)
item=previous;
else item=following;
}
return(item);
}
DATA * edit(DATA *item) // Edit data in list
{
DATA *new;
gotoxy(20,5);
printf(" editing current wave record ");
new = item;
gotoxy(25,10); printf("Amplitude: ");
gotoxy(25,11); printf("Frequency: Hz ");
gotoxy(25,12); printf("Phase: deg ");
gotoxy(40,10); scanf("%lf", &new->amp);
gotoxy(40,11); scanf("%lf", &new->freq);
gotoxy(40,12); scanf("%lf", &new->phase);
return (item);
}
DATA * next(DATA *item) // move to next item in list
{
if (item != NULL)
{
item = item->next;
}
return (item);
}
DATA * find(DATA *item) // locate a particular item in the list
{
gotoxy(20,5);
printf(" Facility not implemented yet! ");
Sleep(1000);
return (item);
}
int main (void) // main function
{
DATA *first=NULL, *current=NULL, *last=NULL;
char response;
system("cls");
do // repeat until "q" hit
{
print_data(current); // print current item in list to screen
gotoxy(25,19); printf("'d': Delete current inputted data"); // display command options
gotoxy(25,18); printf("'a': Adding a new sine wave data");
gotoxy(25,20); printf("'f': find");
gotoxy(25,21); printf("'n': browse the next record on the database");
gotoxy(25,22); printf("'r': return to start of list");
gotoxy(25,23); printf("'e': edit current record");
gotoxy(25,24); printf("'q': quit the database");
gotoxy(40,25); putch(' '); // overwrite old command
gotoxy(25,25);
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 = del_data(current); break;
case 'a': last = add_data(last); break;
case 'f': current = find(first); break;
case 'n': current = next(current); break;
case 'r': current = first; break;
case 'e': current = edit(current); break;
}
if (first == NULL) current = first = last; // assign pointers to first
// item provided in list
} while (response != 'q');
system("cls");
return 0;
}