Did some reading, but I can't grasp the concepts. I realize that you need to use malloc() on a new pointer but I get confused in the transitions to functions. I could really use any help. Any advice on how I am doing this wrong?
Code:
#include <stdlib.h>
#include <stdio.h>
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 AddToBeginning(struct point *ptrF, struct point *ptrL);
void AddToEnd(struct point *ptrF, struct point *ptrL);
void InputRecord(struct point *ptrNew); // used by Add to interactively get the values from the user
int isEmptyList(struct point *ptrF)//emptylist will determine if the list is empty
{
{
if(ptrF==NULL)
{
return 0;
}
else
return 1;
}
}
//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)
{
free(ptrF);
free(ptrL);
}
//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):\n");
ptrFirst=ptrNew;
if(ptrNew==NULL)
printf("No more memory.\n");
scanf("%d%d",ptrFirst->x, ptrFirst->y);
printf("Please enter the label:\n");
scanf("%s", ptrFirst->label);
ptrFirst->ptrNext=NULL;
}
struct point *ptrFirst = NULL;
struct point *ptrLast = NULL;
struct point *ptrNew = 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)
{
ptrNew=(struct point *)(malloc(sizeof(struct point)));
AddToEnd(ptrFirst,ptrLast);
}
if(input==2)
{
ptrNew=(struct point *)(malloc(sizeof(struct point)));
AddToBeginning(ptrFirst,ptrLast);
}
if(input==3)
{
ptrNew=(struct point *)(malloc(sizeof(struct point)));
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)
{
ptrNew=(struct point *)(malloc(sizeof(struct point)));
ResetList(ptrFirst,ptrLast);
}
if(input==5)
{
ptrNew=(struct point *)(malloc(sizeof(struct point)));
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");
}
}
It segmentation faults when scanning in the x and y values.