Description

Insertion in Begin singly linked list.

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

struct node
{
    int num;
    struct node *next;
};
typedef struct node NODE;
void traversal(NODE *head){
    while(head!=NULL){
        printf("%d ", head->num);
        head = head->next;
    }
}
int main()
{
    NODE *start=NULL, *ptr, *current;

    int item;
    char ch;
    do
    {
        ptr = (NODE *)malloc(sizeof(NODE));
        printf("\nEnter the Node : ");
        scanf("%d", &item);
        ptr->num = item;
        ptr->next = NULL;
        if (start == NULL)
        {
            start = ptr;
            current = ptr;
        }
        else
        {
            current->next = ptr;
            current = current->next;
        }

        printf("\n Do You want to enter the more node press [y] : ");
        ch = getche();
    } while (ch == 'y');

    // before Insertion
    printf("\nBefore Insertion : ");
    traversal(start);

    // insertion
    int data;
    printf("\nEnter the element : ");
    scanf("%d", &data);

    ptr = (NODE*)malloc(sizeof(NODE));
    ptr->num = data;
    ptr->next = start;
    start = ptr;
    printf("\nAfter Insertion : ");
    traversal(start);
    return 0;
}

Insertion in Last in Singly Linked List.

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

struct node{
    int num;
    struct node *next;
};

typedef struct node NODE;

int main(){

    NODE *start=NULL, *temp, *ptr, *current;
    // Make Linked List;

    int item;
    char ch;

    do{
        ptr = (NODE *)malloc(sizeof(NODE));
        printf("\nEnter the item of Node do you want to Enter : ");
        scanf("%d", &item);
        ptr->num = item;
        ptr ->next = NULL;
        
        if(start==NULL){
            start = ptr;
            current = ptr;
        }
        else{
            current->next = ptr;
            current = current->next;
        }
        printf("\nDo you want to Enter the More Node then Press [y/Y] : ");
        scanf("%s", &ch);
    }while(ch=='y' || ch=='Y');

    // print the element
    printf("\n Before Insertion in Last \n");
    temp = start;
    while (temp!=NULL)
    {
        printf("%d ", temp->num);
        temp = temp->next;
    }


    // insert the node at the end;
    NODE *p=start;
    while (p->next!=NULL)
    {
        p = p->next;
    }

    ptr = (NODE *)malloc(sizeof(NODE));
    int data;
    printf("\nEnter the Item do you want to insert the node : ");
    scanf("%d", &data);

    ptr->num = data;
    p->next=ptr;
    ptr->next=NULL;

    printf("\n After Insertion in Last \n");
    NODE *t;
    t = start;
    while (t!=NULL)
    {
        printf("%d ", t->num);
        t = t->next;
    }
    
    
    return 0;
}

All Operation of Insertion using function.

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

struct node {
    int num;
    struct node *next;
};

typedef struct node NODE;

void traves(NODE *head){
    while(head!=NULL){
        printf("%d ", head->num);
        head = head->next;
    }
}

void insertBegin(NODE *head, int data){
    NODE *ptr;
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr->num = data;
    ptr->next = head;
    head = ptr; 
    printf("\nAfter Insertion \n");
    traves(head);
}

void insertLast(NODE *head, int data){
    NODE *ptr;
    ptr = (NODE*)malloc(sizeof(NODE));

    NODE *temp;
    temp = head;

    ptr -> num = data;
    ptr->next = NULL;
    while(temp->next!=NULL){
        temp = temp->next;
    }
    temp ->next = ptr;
    printf("\nAfter Insertion IN Last\n");
    traves(head);
}

void insertPerticualarPos(NODE *head, int data, int loc){
     NODE *temp = head, *ptr;
     ptr = (NODE*)malloc(sizeof(NODE));
     ptr->num = data; 
     ptr->next = NULL;    
     int i=1;
     while(i<loc-1){
         temp = temp->next;
         i++;
     }
     ptr -> next = temp->next;
     temp -> next = ptr;

     printf("\nAfter insertion at perticular Pos \n");
    temp=head;
    traves(temp);


}

int main(){
    NODE *start=NULL, *ptr, *current;

    // create NODE
    int item;
    char ch;

    do
    {
        // create node
        ptr = (NODE*)malloc(sizeof(NODE));
        printf("Enter the element : ");
        scanf("%d", &item);
        ptr -> num = item;
        ptr->next = NULL;

        if(start==NULL){
            start = ptr;
            current = ptr;
        }
        else{
            current->next = ptr;
            current = current->next;
        }

        // if want add more node

        printf("\nDo You want To add More NODE press[y/Y] : ");
        scanf("%s", &ch);

    } while (ch=='y' || ch=='Y');


    char s;  
    do
    {
    int choice;
    int data;
    printf("\n1.Display\n2.Insert In the Begin\n3.Insert In the Last\n4.Insert in the specified Pos\nSelect Any One \n");

    scanf("%d", &choice);
         switch (choice)
    {
    case 1:
        traves(start);
        break;
    case 2:
         
         printf("Enter the data do you want to insert the starting Pos : ");
         scanf("%d", &data);
         insertBegin(start, data);
         break;
    case 3:
         printf("Enter the data do you want to insert the Last pos : ");
         scanf("%d", &data);
         insertLast(start, data);
         break;
    case 4:
         printf("Enter the data do you want to insert specified Postition : ");
         scanf("%d", &data);
         int loc;
         printf("\nEnter the position where you want to insert the element : ");
     scanf("%d", &loc);
         insertPerticualarPos(start, data, loc);
         break;
    default:
        break;
    }
    printf("\nDo You want to do More Operation press[y/Y] : ");
    scanf("%s", &s);
    } while (s=='Y' || s=='y');
    
    
    return 0;
}

Insertion and Deletion All operation at the singly linked list in C programming in one program.

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

struct node{
    int num;
    struct node *next;
};

typedef struct node NODE;

NODE *start=NULL, *ptr, *current;
void display();
void create(int item);
void insertion_begin(int item);
void insertion_last(int item);
void insertion_at_pos(int loc, int item);
void deletion_begin();
void deletion_last();
void deletion_at_pos(int loc);


int main(){
    int item;
    
    for(int i=1; i<=5; i++){
        printf("Enter the item : ");
        scanf("%d", &item);
        create(item);
    }
    display(start);
    
    printf("Enter the item new node : ");
    scanf("%d", &item);
    insertion_begin(item);
    printf("\nAfter Insetion\n");
    display(start);
    
    
    insertion_last(item);
    printf("\nAfter Insetion\n");
    display(start);
    
    
    insertion_at_pos(3,item);
    printf("\nAfter Insetion\n");
    display(start);
    
    deletion_begin();
    printf("\nAfter deletion\n");
    display(start);
    
    deletion_last();
    printf("\nAfter deletion\n");
    display(start);
    
    deletion_at_pos(3);
    printf("\nAfter deletion\n");
    display(start);
    
    
    return 0;
}

void display(NODE *start){
    NODE *temp = start;
    int i = 1;
    while(temp!=NULL){
        printf("Element %d : %d\n", i, temp->num);
        temp = temp->next;
        i++;
    }
}

void create(int item){
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr -> num = item;
    ptr -> next = NULL;
    
    if(start==NULL){
        start = ptr;
        current = ptr;
    }
    else{
        current->next =ptr;
        current = current->next;
    }
}

void insertion_begin(int item){
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr -> num = item;
    ptr ->next = start;
    start = ptr;
}
void insertion_last(int item){
    NODE *temp = start;
    
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr -> num = item;
    ptr ->next = NULL;
    while(temp->next!=NULL){
        temp = temp->next;
    }
    
    temp -> next = ptr;
}

void insertion_at_pos(int loc, int item){
    NODE *temp = start;
    ptr = (NODE*)malloc(sizeof(NODE));
    ptr -> num = item;
    ptr -> next = NULL;
    
    int i = 1;
    while(i<loc-1){
        temp = temp ->next;
        i++;
    }
    NODE *p = temp ->next;
    temp -> next = ptr;
    ptr -> next = p;
    
}

void deletion_begin(){
    NODE *temp=start->next;
    
    free(start);
    start = temp;
    
}

void deletion_last(){
    NODE *temp = start;
    while(temp->next->next!=NULL){
        temp= temp->next;
    }
    
    NODE *p = temp->next;
    free(p);
    temp->next=NULL;
}
void deletion_at_pos(int loc){
    NODE *temp = start;
    
    int i = 1;
    while(i<loc-1){
        temp = temp->next;
        i++;
    }
    NODE *p = temp->next;
    temp->next = p->next;
    free(p);
}

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Simple Macro Substitution(#define) in C and C++ programming language

Program to Explain Simple Macro Substitution ( #define ) c and c++ programming language

Print numbers from 1 to 100 using while loop c and cpp program

C program to print numbers from 1 to 100 using while loop and also c++ program.

How to make swastik in c and cpp programming language

Write a c and c++ program to print the Swastik. How to make Swastik in c and cpp.

User Login System in C programming language

We create a login System program which takes username and password from the user.

Program to print a message without using semicolon in c programming

How to write a c program to print a message without using semicolon in c programming language.

Function call by value in c programming language

The call by value technique of passing arguments to a function copies the particular value of an argument into the formal parameter of the function.