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!

Dani AI

Generated

Brief summary tying the fixes together (for future readers):

’s head-insert advice solved the insertion symptom; the remaining crashes and “strange” numbers come from two separate issues: newly allocated nodes sometimes have an uninitialized next field, and the pop code prints/uses the node after or instead of printing the node’s stored integer. An uninitialized next will make a traversal follow garbage (leading to an AccessViolation). Printing the node pointer rather than the node’s num, or accessing a node after it’s freed, produces the odd values seen.

Concrete checks and practices to make the list robust

  • Always initialize new node fields. Zero-allocation (e.g., calloc) or explicitly set next to a known value prevents accidental traversal into garbage.
  • On pop: copy the node’s integer into a local int before calling free, then print that local value. Never print or dereference a pointer that has been freed.
  • Check malloc/calloc return values.
  • Put a break at the end of each case in switch and use int main(...) that returns a value.
  • Prefer small push/pop/print functions instead of doing all logic inline in main. That reduces copy/paste mistakes and makes testing easier.
  • Use a memory tool (Valgrind or AddressSanitizer) to catch invalid reads/writes and use-after-free bugs.

Minimal safe pop helper (illustrative)

int pop_head(Rec **head_ptr, int *out_value) {
    if (!head_ptr || !*head_ptr) return 0;
    Rec *node = *head_ptr;
    *head_ptr = node->next;
    if (out_value) *out_value = node->num;
    free(node);
    return 1;
}

Suggested verification sequence: insert A, insert B, print (expect B then A); pop (expect B); pop (expect A); pop again (expect “empty” behavior). This will confirm both initialization and pop-ordering are correct.

Recommended Answers

All 3 Replies

I think the problem is line 36. freenode is the new node to add to the head of the linked list. all you have to do is this:

freenode->next = top;
top = freenode;

line 45: you need a break statement before the next case.

line 11: main() always always, always returns int. Some compilers will allow void return but that is a compiler extension.

Thanks! That worked. For the delete function, I want to delete the first node in the list. It seemes like it does that but when I go to print it out, i get a strange number, not the number i got rid of. Also if I add numbers to the list and then delete them, when I get to the empty list, I get an AccessViolation.

`

#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;
  Rec *top, *freeNode, *current;

  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
        {
            freeNode -> next = top;
            top = freeNode;
        }
        break;


     case 2:
         current = top;
         if (top == NULL)
             printf("List is Empty");
         else{
         top = current -> next;
         printf("%d", current);
         free(current);
         }
         break;







  }


}

`

line 23: after allocating the new node you need to set the next pointer to NULL so that the program can detect the end of the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.