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!

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.