Home > OS >  Implementing linked list in C
Implementing linked list in C

Time:11-11

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.

CodePudding user response:

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  ;
  • Related