0

I am trying to implement linked list in C, having options for creation, insertion, deletion and display. My code is:

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

void createFirst(int);
void appendNode(int);
void insertFirst(int);
void insertNode(int,int);
void deleteFirst();
void deleteNode(int);
void display();

struct Node
{
    int data;
    struct Node *link;
};

typedef struct Node Node;

Node *start = NULL;
int count=0;

void main()
{
    int ch;
    do
    {
        printf("\f\n");
        printf("1. Create the list \n");
        printf("2. Insert an element at any position \n");
        printf("3. Delete an element at any position \n");
        printf("4. Display the list \n");
        printf("5. Quit \n");
        printf("Enter your choice : \n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
            {
                int a;
                char c;
                printf("Enter the data : \n");
                scanf("%d",&a);
                createFirst(a);
                while(1)
                {
                    printf("Do you want to continue[Y/N] : \n");
                    scanf("  %c",&c);
                    if(c=='Y' || c=='y')
                    {
                        printf("Enter the data : \n");
                        scanf("%d",&a);
                        appendNode(a);
                    }
                    else if(c=='N' || c=='n')
                    {
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                break;
            }
            case 2:
            {
                int a,pos;
                char c;
                printf("Enter the data : \n");
                scanf("%d",&a);
                printf("Enter the position : \n");
                scanf("%d",&pos);
                if(pos == 1)
                {
                    insertFirst(a);
                }
                else
                {
                    insertNode(pos,a);
                }
                while(1)
                {
                    printf("Do you want to continue[Y/N] : ");
                    scanf(" %c",&c);
                    if(c=='N' || c=='n')
                    {
                        break;
                    }
                    if(c!='Y' && c!='y')
                    {
                        continue;
                    }
                    printf("Enter the data : \n");
                    scanf("%d",&a);
                    printf("Enter the position : \n");
                    scanf("%d",&pos);
                    if(pos == 1)
                    {
                        insertFirst(a);
                    }
                    else
                    {
                        insertNode(pos,a);
                    }
                }
                break;
            }
            case 3:
            {
                int pos;
                char c;
                printf("Enter the position : \n");
                scanf("%d",&pos);
                if(pos == 1)
                {
                    deleteFirst();
                }
                else
                {
                    deleteNode(pos);
                }
                while(1)
                {
                    printf("Do you want to continue[Y/N] : ");
                    scanf(" %c",&c);
                    if(c=='N' || c=='n')
                    {
                        break;
                    }
                    if(c!='Y' && c!='y')
                    {
                        continue;
                    }
                    printf("Enter the position : \n");
                    scanf("%d",&pos);
                    if(pos == 1)
                    {
                        deleteFirst();
                    }
                    else
                    {
                        deleteNode(pos);
                    }
                }
                break;
            }
            case 4:
            {
                display();
                break;
            }
            case 5:
            {
                return;
            }
            default:
            {
                printf("Invalid choice \n");
                break;
            }
        }
    }while(ch!=5);
}

void createFirst(int d)
{
    Node newnode = {d,NULL};
    start = &newnode;
    count++;
}

void appendNode(int d)
{
    Node temp = *start;
    while(temp.link != NULL)
    {
        temp = *temp.link;
    }
    Node newnode = {d,NULL};
    temp.link = &newnode;
    count++;
}

void insertFirst(int d)
{
    Node newnode = {d,NULL};
    newnode.link = start;
    start = &newnode;
    count++;
}

void insertNode(int n,int d)
{
    if(n>count || n<count)
    {
        printf("Invalid position \n");
        return;
    }
    Node temp = *start;
    int i;
    for(i=1;i<n-1;i++)
    {
        temp = *temp.link;
    }
    Node newnode = {d,NULL};
    newnode.link = temp.link;
    temp.link = &newnode;
}

void deleteFirst()
{
    if(start != NULL)
    {
        printf("Deleted element : %d \n",(*start).data);
        start = (*start).link;
        count--;
    }
    else
    {
        printf("Underflow \n");
    }
}

void deleteNode(int n)
{
    if(n>count || n<count)
    {
        printf("Invalid position \n");
        return;
    }
    Node temp = *start;
    int i;
    for(i=1;i<n-1;i++)
    {
        temp = *temp.link;
    }
    printf("Deleted node : %d",temp.link->data);
    temp = *(temp.link->link);
    count--;
}

void display()
{
    if(start == NULL)
    {
        printf("The list is empty \n");
        return;
    }
    Node temp = *start;
    int i;
    for(i=1;i<=count;i++)
    {
        printf("%d ",temp.data);
        temp = *temp.link;
    }
}

But:

  1. Whenever the control is going to appendNode function, the program terminates somehow.
  2. After only creating the first node, if i go to display, it is printing some garbage value.

Please somebody help.

3
  • 1
    first problem, your createFirst function creates a node in automatic memory which dies as soon as createFirst exits, meaning that start points to nothing after that. You need to malloc memory for the node in order for it to persist after the function call. It looks like this is a common problem throughout your code. Commented Nov 11, 2021 at 5:27
  • 1
    This is tagged C++, but the concept of what you're doing is exactly the same: stackoverflow.com/questions/6441218/… Commented Nov 11, 2021 at 5:30
  • Okay. Thanks @yano I will try it too. Commented Nov 11, 2021 at 5:39

1 Answer 1

1

Your problem is on the line (inside of appendNode) which contains this: Node temp = *start;. Don't do it that way. Do it this way:

Node *temp = start;

Change the following pointer notations to accomodate this change. Like change the dot operators to the -> operator. Here's the rest of the function:

while(temp->link != NULL)
{
    temp = temp->link;
}
Node *newnode = malloc (sizeof(struct Node));
newnode->data = 0;
newnode->link = NULL;

temp->link = newnode;
count++;
Sign up to request clarification or add additional context in comments.

4 Comments

Would you mind providing an explanation of your answer?
Ok thanks @ADBeveridge I will try.
Thank you thanks a lot @ADBeveridge . It really helped.
Thank you thanks a lot @yano. It really helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.