Linked List operation using C programing || Code With Rudy

 Linked List operation using C programing


Copy the code given below and Run in your compiler!

Linked List in C Programming

A linked list is a linear data structure where each element is a separate object, known as a node. Each node contains two items: the data and a reference (or link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence.

Components of a Linked List

  1. Node: A node is the basic building block of a linked list. It contains two items: the data and a reference to the next node.
  2. Head: The head is the first node in the linked list.
  3. Tail: The tail is the last node in the linked list.

Types of Linked Lists

  1. Singly Linked List: In a singly linked list, each node only points to the next node.
  2. Doubly Linked List: In a doubly linked list, each node points to both the previous and next nodes.
  3. Circularly Linked List: In a circularly linked list, the last node points back to the first node.

Linked List Operations

  1. Insertion: Inserting a new node at a specific position in the linked list.
  2. Deletion: Deleting a node from the linked list.
  3. Traversal: Traversing the linked list to access each node.

Time Complexity

  • Insertion: O(1) (amortized)
  • Deletion: O(n) (worst-case)
  • Traversal: O(n)

Space Complexity

  • O(n) (where n is the number of nodes in the linked list)

Note: The time and space complexities assume a singly linked list. The complexities may vary for doubly linked lists or circularly linked lists.




Code With Rudy
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
}*start=NULL,*q,*t;
int main(){
    int ch;
    void insert_beg();
    void insert_end();
    int insert_pos();
    void display();
    void delete_beg();
    void delete_end();
    int delete_pos();
    while(1){
        printf("\n\n---- Singly Linked List(SLL) Menu ----");
        printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
        printf("Enter your choice(1-4):");
        scanf("%d",&ch);
 
        switch(ch){
            case 1:
                    printf("\n---- Insert Menu ----");
                    printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit");
                    printf("\n\nEnter your choice(1-4):");
                    scanf("%d",&ch);
 
                    switch(ch){
                        case 1: insert_beg();
                                break;
                        case 2: insert_end();
                                break;
                        case 3: insert_pos();
                                break;
                        case 4: exit(0);
                        default: printf("Wrong Choice!!");
                    }
                    break;
 
            case 2: display();
                    break;
 
            case 3: printf("\n---- Delete Menu ----");
                    printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified position\n4.Exit");
                    printf("\n\nEnter your choice(1-4):");
                    scanf("%d",&ch);
 
                    switch(ch){
                        case 1: delete_beg();
                                break;
                        case 2: delete_end();
                                break;
                        case 3: delete_pos();
                                break;
                        case 4: exit(0);
                        default: printf("Wrong Choice!!");
                    }
                    break;
            case 4: exit(0);
                    default: printf("Wrong Choice!!");
        }
    }
    return 0;
}
void insert_beg(){
    int num;
    t=(struct node*)malloc(sizeof(struct node));
    printf("Enter data:");
    scanf("%d",&num);
    t->data=num;
 
    if(start==NULL){
        t->next=NULL;
        start=t;
    }
    else{
        t->next=start;
        start=t;
    }
}
void insert_end(){
    int num;
    t=(struct node*)malloc(sizeof(struct node));
    printf("Enter data:");
    scanf("%d",&num);
    t->data=num;
    t->next=NULL;
    if(start==NULL){
        start=t;
    }
    else{
        q=start;
        while(q->next!=NULL)
        q=q->next;
        q->next=t;
    }
}
int insert_pos(){
    int pos,i,num;
    if(start==NULL){
        printf("List is empty!!");
        return 0;
    } 
    t=(struct node*)malloc(sizeof(struct node));
    printf("Enter data:");
    scanf("%d",&num);
    printf("Enter position to insert:");
    scanf("%d",&pos);
    t->data=num;
    q=start;
    for(i=1;i<pos-1;i++){
        if(q->next==NULL)
        {
            printf("There are less elements!!");
            return 0;
        }
 
        q=q->next;
    }
    t->next=q->next;
    q->next=t;
    return 0;
} 
void display(){
    if(start==NULL){
        printf("List is empty!!");
    }
    else{
        q=start;
        printf("The linked list is:\n");
        while(q!=NULL){
            printf("%d->",q->data);
            q=q->next;
        }
    }
}
void delete_beg(){
    if(start==NULL){
        printf("The list is empty!!");
    }
    else{
        q=start;
        start=start->next;
        printf("Deleted element is %d",q->data);
        free(q);
    }
}
void delete_end(){
    if(start==NULL){
        printf("The list is empty!!");
    }
    else{
        q=start;
        while(q->next->next!=NULL)
        q=q->next;
 
        t=q->next;
        q->next=NULL;
        printf("Deleted element is %d",t->data);
        free(t);
    }
}
int delete_pos(){
    int pos,i;
    if(start==NULL){
        printf("List is empty!!");
        return 0;
    }
    printf("Enter position to delete:");
    scanf("%d",&pos);
    q=start;
    for(i=1;i<pos-1;i++){
        if(q->next==NULL)
        {
            printf("There are less elements!!");
            return 0;
        }
        q=q->next;
    }
    t=q->next;
    q->next=t->next;
    printf("Deleted element is %d",t->data);
    free(t);
    return 0;
} 

Post a Comment

0 Comments