I'm having a problem with my linked list. I want to add to the beginning of the list and delete from the beginning of the list. I get an AccessViolation which is coming from where I print out the list. I also don't think it's adding elements to the linked list, it just overwrites whats there already. I don't know if the delete function works but when I run it, I get a NullPointer Exception.
#include <StdAfx.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
// This struct is complete. Do not change it.
int num;
struct Node *next;
} Rec;
void main(){
// Complete this function
int x = 0;
int y = 0;
int k;
Rec *top, *freeNode, *current, *temp;
top = NULL;
while(x != 3){
printf("Enter 1 to push a number, 2 to pop, and 3 to quit: ");
scanf("%d", &x);
switch(x){
case 1:
freeNode = (Rec*)malloc(sizeof(Rec));
printf("Enter an integer to push: ");
scanf("%d", &freeNode -> num);
if(top ==NULL)
{
top = freeNode;
current = freeNode;
}
else
{
current -> next = freeNode;
top = freeNode;
}
temp = top;
while(temp != NULL)
{
printf("%d", temp -> num);
temp = temp -> next;
}
case 2:
current = top;
if (current == NULL)
printf("List is Empty");
top = current -> next;
free(current);
}
}
Thanks in advance!