I am creating my first program using dynamic memory allocation, I have started it but can't get past how far I am now. I was just looking for advice on how to fix up what I have, don't need any tips for the file part yet, just looking to understand what I have now. Yes this is homework but I have honestly tried and done a lot myself, I just need a push in the right direction. This is my code:
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 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)
{
if(ptrF=NULL)
return 0;
else
return 1;
}
//print list will print out everything
void PrintList(struct point *ptrF)
{
if(ptrF!=NULL)
{
printf("label: %s\nx: %d y: %d\n",ptrF->label,ptrF->x,ptrF->y);
PrintList(ptrF->ptrNext);
}
}
//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;
InputRecord(&ptrNew);
ptrNew.ptrNext=ptrF;
ptrF=&ptrNew;
}
//addtoend will add a number to the end
void AddToEnd(struct point *ptrF, struct point *ptrL)
{
if(ptrF==NULL)
InputRecord(ptrF);
else
{
struct point New;
InputRecord(&New);
ptrL=&New;
}
}
//reads in input
void InputRecord(struct point *ptrNew)
{
printf("Please enter the 2 Data points(x and y)");
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 (reset/replace current memory content)\n"
"0. Exit\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");
}
else
{
printf("The list is empty");
}
}
if(input==4)
{
ResetList(ptrFirst,ptrLast);
}
if(input==5)
{
PrintList(ptrFirst);
}
if(input==6)
{
}
if(input==7)
{
}
if(input==0)
{
input=0;
}
else
printf("Invalid entry, try again\n");
}
}
My first problem is that when asking to scan in the first "input" from the user it gives segmentation fault(core dumped)