-1

How do I fix error I keep getting when I run the program? Im trying to make head point to the address of first and then be able to pass *first through a function where a new node will be created, the user can give it data at run-time, and then first will point to the new node! Am I doing this right?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);

struct node{

    int data;
    struct node *next;

};


void main(){

    int option = 0;

    struct node *head;
    struct node *first;
    head = (struct node*)malloc(sizeof(struct node));
    first = (struct node*)malloc(sizeof(struct node));

    head->data= 0;
    head->next = first;
    first->data = 1;
    first->next = NULL;

    Menu();
    scanf(" %d", option);

    while(option != 6){
        switch(option){
        case 1:
            addToStart(&first);
            break;
        case 3:
            DisplayList(head);
        break;
    case 6:
        exit(0);
        break;

    default:
        printf("\nTry Again");
        break;
        }//switch end
    }//while end

}

void addToStart (struct node** first)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node");
scanf("%d", &newNode->data);
newNode->next = *first;
*first = newNode; // transfer the address of newNode' to 'head'
}

void Menu(){

    printf("1) Add a node.\n");
    printf("3) Display all nodes.\n");
    printf("6) Exit.\n");


}

void DisplayList(struct node* head){

    struct node *temp;
    temp =(struct node*)malloc(sizeof(struct node));
    temp = head;
    while( temp!= NULL )
    {
        printf("Data: %d", temp->data); // show the data
        temp = temp->next;
    }
}
conor tighe.
  • 121
  • 2
  • 19
  • where and what error, please? – Sourav Ghosh Mar 09 '15 at 14:12
  • 1
    `temp =(struct node*)malloc(sizeof(struct node)); temp = head;` This is probably unrelated to your error, but if you're setting `temp` to `head` then the first line is unnecessary and leads to a memory leak. – Daniel Kleinstein Mar 09 '15 at 14:13
  • and `addToStart(&first);` --> `addToStart(&head);` ?, `while(option != 6){` : never update `option` – BLUEPIXY Mar 09 '15 at 14:25
  • What do you mean by "unhandled exception"? C++ has exceptions; C doesn't. Please update your question to show the exact error message you're getting. Also, if some instructor, book, or tutorial told you to use `void main()` find a better one; it's `int main(void)`. (Some compilers will accept `void main()`, but there's no good reason to use it.) – Keith Thompson Mar 09 '15 at 14:49

2 Answers2

0
#include <stdio.h>
#include <stdlib.h>


struct node{

    int data;
    struct node *next;

};

void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);




int main(){

    int option = 0;

    struct node *head;
    //struct node *first;
    //head = (struct node*)malloc(sizeof(struct node));
    //first = (struct node*)malloc(sizeof(struct node));

    head = NULL;

    option = 0;
    while(option != 6){
        Menu();
        scanf(" %d", &option);
        switch(option){

            case 1:
                addToStart(&head);
                                break;
            case 3:
                DisplayList(head);
                                break;
            case 6:
                exit(0);

                break;

            default:
                printf("\nTry Again");
                break;
        }//switch end
    }//while end

}

void addToStart (struct node** head)
{
    struct node *newNode;
    newNode = (struct node*)malloc(sizeof(struct node));
    printf("\nEnter data for this node");
    scanf("%d", &newNode->data);
    newNode->next = NULL;
    if (*head==NULL)
    {
        *head = newNode;
    }
    else
    {
        struct node *lastNode = *head;
        while(lastNode->next!=NULL)
            lastNode = lastNode->next;
        lastNode->next = newNode;


    }

   // *first = newNode; // transfer the address of newNode' to 'head'
}

void Menu(){

    printf("\n1) Add a node.\n");
    printf("3) Display all nodes.\n");
    printf("6) Exit.\n");


}

void DisplayList(struct node* head){

    struct node *temp;
    temp =(struct node*)malloc(sizeof(struct node));
    temp = head;
    while( temp!= NULL )
    {
        printf("Data: %d", temp->data); // show the data
        temp = temp->next;
    }
}

This works.

Many points you should consider:

1) it is int main, not void main()

2) give address of variable you want to set value for when you do scanf. it should be scanf(" %d", &option); not scanf(" %d", option);

3) When you made a new node, you were not setting its next to NULL. This was missing. newNode->next = NULL;

4) You were in infinite loop. option was never updated. I added that. Also, Menu should be shown after user has given his choice.

5) There is no check for memory allocation. What if malloc returns NULL?

6) You were getting confused with first and head. Convention is to use head.

7) I would use typedef , instead of struct node everytime. This is left as an exercise :)

OP, this list is not exhaustive.

If you want to display numbers in reverse order, change the else part in addToStart like this

else
    {
        newNode->next = *head;
        *head = newNode;
    }
Anon
  • 2,608
  • 6
  • 26
  • 38
  • 3) : `next` has been set. – BLUEPIXY Mar 09 '15 at 14:39
  • your `addToStart` is `addToLast`. – BLUEPIXY Mar 09 '15 at 14:43
  • @BLUEPIXY , yes . Did not realize it. I guess it is good in a way since it is clear OP is still learning the basics. I gave addToLast, OP should be able to figure out addToStart. – Anon Mar 09 '15 at 14:44
  • Thanks for the detailed answer, I'll try work with this and hopefully ill make some progress! – conor tighe. Mar 09 '15 at 14:49
  • By the way how come we dont allocate new memory with malloc for head? I was thought this is how we declare a node using a struct to make sure theres memory at runtime? – conor tighe. Mar 09 '15 at 14:58
  • @conortighe. , actually, head is pointing to an allocated memory location. When head is NULL, head points to memory allocated by newNode. I would suggest draw a diagram, dump the addresses of newNode and head and see. That way you will learn more. To dump - use &head and &newNode. – Anon Mar 09 '15 at 15:06
  • @conortighe., Please accept this answer if it helped you. I see you are new to StackOverflow so it is understandable if someone shows you the rope here. Thanks. – Anon Mar 09 '15 at 15:18
  • What exactly do you mean by dump? pass through functions as &head? – conor tighe. Mar 09 '15 at 15:56
  • @conortighe., Sorry to have used lingo, I should have used print. Here is what I meant. printf("%u,%u",&head,&newNode); do this in addToStart function, just before it is exiting. & gives the address as you already know. You will see how head and NewNode point to same address location initially. Head is just a pointer to a memory location. So is newNode. Allocating just one is enough. – Anon Mar 09 '15 at 16:14
  • @conortighe., if things are not clear even after that, do not hesitate to ask questions. I can see you want to learn, which is always a good thing. :) – Anon Mar 09 '15 at 16:26
  • Thanks! by the way is %u giving you the address its pointing at? – conor tighe. Mar 09 '15 at 17:03
  • @Anon: No, don't use `%u` to print addresses. `%u` requires an argument of type `unsigned int`. `%p` takes a `void*` pointer. To print an address value: `printf("%p, %p\n", (void*)&head, (void*)&newNode)` – Keith Thompson Mar 09 '15 at 18:39
  • @conortighe., You get the address because of "&" in front of the variables. %u, %d, %f, %p are format specifiers. U is for unsigned integers, F for float etc. So there is nothing like printing with %u gives address. You could use printf("%p, %p\n", (void*)&head,(void*)&newNode) and it would work too. – Anon Mar 10 '15 at 12:45
0
#include <stdio.h>
#include <stdlib.h>


struct node{

    int data;
    struct node *next;

};

void addToStart (struct node** head);
void addToEnd (struct node** head);
void Menu();
void Size(struct node* head);
void DisplayList(struct node* head);
void SearchList(struct node* head);



int main(){

    int option = 0;

    struct node *head;
    struct node *first;
    head = (struct node*)malloc(sizeof(struct node));
    first = (struct node*)malloc(sizeof(struct node));

    head = NULL;

    option = 0;
    while(option != 6){
        Menu();
        scanf(" %d", &option);
        switch(option){

            case 1:
                addToStart(&head);
                                break;
            case 2:
                addToEnd(&head);
                break;
            case 3:
                DisplayList(head);
                                break;
            case 4:
                Size(head);
                break;
            case 5:
                SearchList(head);
                break;
            case 6:
                free(head);
                free(first);
                exit(0);
                break;

            default:
                printf("\nTry Again");
                break;
        }//switch end
    }//while end

}

void addToStart (struct node** head)
{
    struct node *newNode;
    newNode = (struct node*)malloc(sizeof(struct node));
    printf("\nEnter data for this node:\n");
    scanf("%d", &newNode->data);
    newNode->next = NULL;
    if (*head==NULL)
    {
        *head = newNode;
    }
    else
    {
         newNode->next = *head;
        *head = newNode;

    }
    printf("%u,%u",&head,&newNode);
   // *first = newNode; // transfer the address of newNode' to 'head'
}

void addToEnd (struct node** head)
{
    struct node *newNode;
    newNode = (struct node*)malloc(sizeof(struct node));
    printf("\nEnter data for this node:\n");
    scanf("%d", &newNode->data);
    newNode->next = NULL;
    if (*head==NULL)
    {
        *head = newNode;
    }
    else
    {
         struct node *lastNode = *head;
         while(lastNode->next != NULL){
                lastNode = lastNode->next;
             lastNode->next = newNode;
         }

        *head = newNode;

    }

  // *first = newNode; // transfer the address of newNode' to 'head'
}

void Size(struct node* head)
    {
        int len = 0;

        struct node *temp;
        temp =(struct node*)malloc(sizeof(struct node));
        temp = head;

        while(temp != NULL)
        {
           len++;
           temp = temp->next;

        }
      printf("Size of list:  %d", len);
    }


void Menu(){

    printf("\n1) Add a node.\n");
    printf("2) Add node to end.\n");
    printf("3) Display all nodes.\n");
    printf("4) Display the length of list.\n");
    printf("5) Search the list.\n");
    printf("6) Exit.\n");


}

void DisplayList(struct node* head){

    struct node *temp;
    temp =(struct node*)malloc(sizeof(struct node));
    temp = head;
    while( temp!= NULL )
    {
        printf("Data: %d  ", temp->data); // show the data
        temp = temp->next;
    }
}



void SearchList(struct node* head){

    int keynum;
    struct node *temp;
    temp =(struct node*)malloc(sizeof(struct node));
    temp = head;

    printf("\nEnter number:\n");
    scanf(" %d", &keynum);

    if(temp->data  == keynum){
        printf("\n%d was found in the list!\n", keynum);
    }
    else{
        printf("\n%d is not in the list!\n", keynum);

    }
}
conor tighe.
  • 121
  • 2
  • 19
  • Congratulations, this looks neat and tidy :). I see you added new methods by yourself. – Anon Mar 10 '15 at 12:47