There is a problem I am having inside the AddToEnd function.
At this line:
*ptrL->ptrNext = ptrInput;
it gives this error:
lab8.c: In function ‘AddToEnd’:
lab8.c:105: error: request for member ‘ptrNext’ in something not a structure or union
Can anybody tell me why?
P.S. I realized that I do not have to pass ptrFirst and ptrLast, but this is just for the sake of practice with pointers
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// DOCUMENT!!!!!!
struct point
{
int x;
int y;
char label[21];
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
struct point *ptrFirst = NULL;
struct point *ptrLast = NULL;
void main()
{
int choice = 0;
while(1)
{
printf("1. Check if list is empty\n");
printf("2. Print list\n");
printf("3. Reset list\n");
printf("4. Add point to beginning\n");
printf("5. Add point to end\n");
printf("6. Exit\n");
scanf("%d", &choice);
getchar();
while(choice < 0 || choice >6)
{
scanf("%d", &choice);
getchar();
}
switch(choice)
{
case 1: printf("%s", (isEmptyList(ptrFirst) == 0)? "Yes\n" : "No\n"); break;
case 2: PrintList(ptrFirst); break;
case 3: ResetList(ptrFirst, ptrLast); break;
case 4: AddToBeginning(&ptrFirst, &ptrLast); break;
case 5: AddToEnd(&ptrFirst, &ptrLast);break;
case 6: ResetList(ptrFirst, ptrLast); return;
}
}
}
int isEmptyList(struct point *ptrF)
{
return (ptrF == NULL)? 0: 1;
}
void PrintList(struct point *ptrF)
{
struct point *ptrCur = ptrF;
while(ptrCur != NULL)
{
printf("%d %d %s\n", ptrCur->x, ptrCur->y, ptrCur->label);
ptrCur = ptrCur->ptrNext;
}
}
void ResetList(struct point *ptrF, struct point *ptrL)
{
struct point *ptrTemp = ptrF;
while(ptrF != NULL)
{
ptrTemp = ptrF->ptrNext;
free(ptrF);
ptrF = ptrTemp;
}
}
void AddToBeginning(struct point **ptrF, struct point **ptrL)
{
struct point *ptrInput = (struct point *)malloc(sizeof(struct point));
if(ptrInput == NULL)
{
printf("Fail to assign space!\n");
return;
}
ptrInput->ptrNext = *ptrF;
*ptrF = ptrInput;
InputRecord(*ptrF);
}
void AddToEnd(struct point **ptrF, struct point **ptrL)
{
struct point *ptrInput = (struct point *)malloc(sizeof(struct point));
if(ptrInput == NULL)
{
printf("Fail to assign space!\n");
return;
}
*ptrL->ptrNext = ptrInput;
*ptrL = ptrInput;
InputRecord(*ptrL);
}
void InputRecord(struct point *ptrNew)
{
char buffer[20];
printf("Input x: ");
fgets(buffer, 20, stdin);
ptrNew->x = atoi(buffer);
printf("Input y: ");
fgets(buffer, 20, stdin);
ptrNew->y = atoi(buffer);
printf("Input label: ");
fgets(buffer, 20, stdin);
strcpy(ptrNew->label, buffer);
}