2011-03-12 16:15:10 UTC
#include
#include
struct point
{
int x; //point x
int y; //point y
char label[21]; //label of 2 points
struct point *ptrNext; // self referential pointer
};
int isEmptyList(struct point *ptrF);
void PrintList(struct point *ptrF);
void ResetList(struct point *ptrF, struct point *ptrL);
void AddToEnd(struct point *ptrF, struct point *ptrL);
void AddToBeginning(struct point *ptrF, struct point *ptrL);
void InputRecord(struct point *ptrNew); // used by Add to interactively get the values from the user
//emptylist will determine if the list is empty
int isEmptyList(struct point *ptrF)
{
struct point *pD = ptrF;
while(pD != NULL)
{
ptrF = ptrF->ptrNext; /* update the pointer to the beginning of the list to point to the next */
free(pD); /* this (previous) node is erased! */
pD = ptrF; /* set the pD to point to the next node to be deleted */
}
}
//print list will print out everything
void PrintList(struct point *ptrF)
{
struct point *pC = ptrF;
while(pC != NULL)
{
printf("Label: %s\nx: %d y:%d\n", pC->label, pC->x, pC->y);
pC = pC->ptrNext; /* this is how a list is traversed! */
}
}
//reset list will make everything equal to NULL
void ResetList(struct point *ptrF, struct point *ptrL)
{
ptrF= NULL;
ptrL= NULL;
}
//addtobeginning will add a number to the beginning
void AddToBeginning(struct point *ptrF, struct point *ptrL)
{
struct point ptrNew;
if(ptrF==NULL)
*ptrF=ptrNew;
InputRecord(&ptrNew);
ptrNew.ptrNext=ptrF;
ptrF=&ptrNew;
}
//addtoend will add a number to the end
void AddToEnd(struct point *ptrF, struct point *ptrL)
{
struct point ptrNew;
if(ptrF==NULL)
*ptrF = ptrNew;
else
{
InputRecord(&ptrNew);
ptrL=&ptrNew;
}
}
//reads in input
void InputRecord(struct point *ptrNew)
{
printf("Please enter the 2 Data points(x and y)");
ptrNew=(struct point *)(malloc(sizeof(struct point)));
if(ptrNew==NULL)
printf("No more memory.\n");
scanf("%d%d",ptrNew->x, ptrNew->y);
printf("Please enter the label");
scanf("%s", ptrNew->label);
ptrNew->ptrNext=NULL;
}
struct point *ptrFirst = NULL;
struct point *ptrLast = NULL;
void main()
{
int empty=0;//helps determine if list is empty
int input=1;//reads in user input
while(input!=0)
{
printf(
"1. Add a point at the END of the list.\n"
"2. Add a point at the BEGINNING of the list.\n"
"3. Is the list empty?\n"
"4. Erase all points from the list (reset).\n"
"5. Display the list.\n"
"6. Save the list to a sequential file (reset/replace file contents)\n"
"7. Read the list back from a sequential file\n"
"(reset/replace current memory content)\n"
"0. Exit\n\n"
);
scanf("%d", &input);
if(input==1)
{
AddToEnd(ptrFirst,ptrLast);
}
if(input==2)
{
AddToBeginning(ptrFirst,ptrLast);
}
if(input==3)
{
empty=isEmptyList(ptrFirst);
if(empty==1)
{
printf("The list is not empty\n\n");
}
else
{
printf("The list is empty\n\n");
}
}
if(input==4)
{
ResetList(ptrFirst,ptrLast);
}
if(input==5)
{
PrintList(ptrFirst);
}
if(input==6)
{
}
if(input==7)
{
}
if(input==0)
{
input=0;
}
if(input<0)
printf("Invalid entry, try again\n\n");
if(input>7)
printf("Invalid entry, try again\n\n");
}
}