DEV Community

Orion
Orion

Posted on • Edited on

Single Linked List Using C

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

typedef struct Node
{
    int data;
    struct Node *next;
}node;

node *HEAD=NULL;

//Interface Functions
int insert(int value);
int insert_at(int value,int position);
int remove_at(int position);
int search(int value);
void display();

//Internal Functions
node *new_node(int data,node* link);
void get_input(char str[],int* param);
void respond(char name[],int flag);
int length();

void main()
{
    int choice,value,position,flag=0;

    while(1)
    {
        printf("\t\tSINGLE LINKED LIST OPERATIONS\n");
        printf("1.Insert | 2.Insert at | 3.Remove at | 4.Search | 5.Display | 6.Exit\n");

        get_input("Choice",&choice);

        switch(choice)
        {
            case 1:
                    get_input("Value",&value);
                    flag=insert(value);
                    respond("Insertion",flag);
                    break;
            case 2:
                    get_input("Value",&value);
                    get_input("Position",&position);
                    flag=insert_at(value,position);
                    respond("Insertion",flag);
                    break;
            case 3:
                    get_input("Position",&position);
                    flag=remove_at(position);
                    respond("Deletion",flag);
                    break;
            case 4:
                    get_input("Value",&value);
                    if (search(value))
                    printf("Element is at position %d.\n\n",search(value));
                    else
                    printf("Element not found.\n\n");
                    break;
            case 5:
                    display();
                    break;
            case 6:
                    exit(0);
            default:
                    printf("Invalid Choice. Try Again.\n\n");
        }
    }
}

//Internal Functions
node *new_node(int value,node *link)
{
    node *temp=(node *)malloc(sizeof(node));
    if(temp==NULL)
    {
        printf("Memory allocation failure.\n\n");
        exit(0);
    }
    temp->data=value;
    temp->next=link;
    return temp;
}

void get_input(char str[],int *param)
{
    printf("Enter %s: ",str);
    scanf("%d",param);
}

void respond(char operation[],int flag)
{   
    if(flag)
    printf("%s Successful.\n\n",operation);
    else
    printf("%s not Successful.\n\n",operation);
}

int length()
{
    int size;
    node *temp=HEAD;
    if(HEAD==NULL)
    return 0;
    for(size=1;temp->next!=NULL;size++)
    temp=temp->next;
    return size;
}

//Interface Functions
int insert(int value)
{
    node *new=new_node(value,NULL);
    node *temp=HEAD;

    if (HEAD==NULL)
    {
        HEAD=new;
        return 1;
    }
    else
    {
        while(temp->next!=NULL)
        temp=temp->next;

        temp->next=new;
        return 1;
    }
    return 0;
}

int insert_at(int value,int position)
{
    node *new=new_node(value,NULL);
    node *temp=HEAD;


    if (position>length())
    {
        if (length()==0)
        {
            printf("List is empty.\n");
            return 0;
        }
        printf("Position is farther than size of list.\n");
        return 0;
    }

    if (position==1)
    {
        if(HEAD==NULL)
        {
            HEAD=new;
            return 1;
        }

        new->next=HEAD;
        HEAD=new;
        return 1;
    }

    if(position<=length())
    {
        for(int i=1;i<position-1;i++)
        temp=temp->next;

        new->next=temp->next;
        temp->next=new;
        return 1;
    }

    return 0;
}

int remove_at(int position)
{
    node *temp=HEAD;
    node *del;

    if (position<=0)
    {
        printf("Invalid Position.\n");
        return 0;
    }

    if (position>length())
    {
        if (length()==0)
        {
            printf("List is empty.\n");
            return 0;
        }
        printf("Position is farther than size of list.\n");
        return 0;
    }

    if (position==1)
    {
        HEAD=HEAD->next;
        free(temp);
        return 1;
    }

    if (position<=length())
    {
        for(int i=1;i<position-1;i++)
        temp=temp->next;

        del=temp->next;
        if (position==length())
        temp->next=NULL;
        else
        temp->next=del->next;
        free(del);
        return 1;
    }

    return 0;
}

int search(int value)
{
    node *temp=HEAD;
    int pos;

    if (HEAD==NULL)
    {
        printf("List is empty.\n");
        return 0;
    }

    for(pos=1;pos<=length();pos++,temp=temp->next)
    if(temp->data==value)
    return pos;

    return 0;
}

void display()
{
    node *temp=HEAD;

    if(HEAD==NULL)
    {
        printf("List is Empty.\n\n");
        return;
    }

    while(temp->next!=NULL)
    {
        printf("%d -> ",temp->data);
        temp=temp->next;
    }
    printf("%d.\n\n",temp->data);

    return;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
ac000 profile image
Andrew Clayton

In C, you shouldn't cast the return value from malloc(3) et al.

Collapse
 
0orion0 profile image
Orion

Why ?

Collapse
 
ac000 profile image
Andrew Clayton

In C (and we're talking about C and not C++)

1) It's superfluous.
2) Can hide bugs depending upon architecture/compiler etc, where if you don't

#include <stdlib.h>
Enter fullscreen mode Exit fullscreen mode

the return type of malloc may be assumed to be int which may mean you're using a truncated address and that won't end well...