Skip to main content

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

Use below command to resolve problem pip3 install PyQt5

Single Linked List code

 #include<stdio.h>

#include<stdlib.h>
#include<conio.h>
typedef struct node
{
    int data;
    struct node *next;
}node;
node *START;
int size=0;

node* createNode();
void insertAtFirst(int);
void insertAtLast(int);
void insertAtAnyPosition(int,int);
void displayNode();
void deleteLastNode();

node* createNode(){
    node *create=(node*)malloc(sizeof(node));
    return create;
}

void insertAtFirst(int data){
    node *insert;
    if(START==NULL){
        START=createNode();
        START->data=data;
        START->next=NULL;
        size++;
        printf("\n%d is inserted successfully",data);
    }
    else{
        insert=createNode();
        insert->data=data;
        insert->next=START;
        START=insert;
        size++;
        printf("\n%d is inserted successfully",data);

    }
}

void insertAtLast(int data){
    node *last,*visit;
    if(START==NULL){
        START=createNode();
        START->data=data;
        START->next=NULL;
        size++;
        printf("\n%d is inserted successfully",data);
    }
    else{
        visit=START;
        while (visit->next!=NULL)
        {
            visit=visit->next;
        }
        last=createNode();
        last->data=data;
        last->next=NULL;
        visit->next=last;
        size++;
        printf("\n%d is inserted successfully",data);

    }
}
void insertAtAnyPosition(int data,int index){
    node *position;
    node *create;
    int indexCount;
    if(index==0){
        insertAtFirst(data);
    }
    else  if(index<0 || index>size-1 ){
        if(index<0){
            printf("Invalid index");
        }
        else
           printf("\nYou can insert element b/w 0 to %d",size-1);
    }

    else {
        position=START;
        indexCount=0;
        while(indexCount<=index-2){
            position=position->next;
            indexCount++;

        }

        create=createNode();
        create->data=data;
        create->next=position->next;
        position->next=create
        printf("%d is inserted at position %d",create->data,index);
        size++;
    }
}

void displayNode()
{
    node *display;
    if(START==NULL)
        printf("No node is found,Insert your node");
    else{
        display=START;
        while(display!=NULL){
            printf("%d ",display->data);
            display=display->next;
        }
    }
}


void deleteLastNode()
{
    node *last,*secondLast;
    if(START==NULL)
        printf("No node found , Can't delete any node");
    else if(START->next==NULL){
        last=START;
        START=NULL;
        printf("Last node = %d is deleted successfully",last->data);
        free(last);
        size--;
    }
    else{
        last=START;
        while(last->next!=NULL){
            last=last->next;
        }
        secondLast=START;
        while(secondLast->next->next!=NULL)
            secondLast=secondLast->next;
        secondLast->next=NULL;
        free(last);
        size--;
        printf("Last node deleted successfully");

    }
}

int menu(){
    int choice;
    printf("\n1. Insert node as 1st node");
    printf("\n2. Insert node as Last node");
    printf("\n3. Insert node at any Position");
    printf("\n4. Display all node");
    printf("\n5. Delete last node");
    printf("\n6. Exit from program");
    printf("\nEnter your choice :");
    scanf("%d",&choice);
    return choice;
}
int main()
{
    int data,index;
    
    while(1)
    {
    // clrscr();
        system("cls");
        switch(menu()){
            case 1:
                printf("\nEnter Data to insert at first :");
                scanf("%d",&data);
                insertAtFirst(data);
                break;
            case 2:
                printf("\nEnter Data to insert at last :");
                scanf("%d",&data);
                insertAtLast(data);
                break;
            case 3:
                printf("\nEnter Data to insert at any position :");
                scanf("%d",&data);
                printf("\nEnter index value to insert at any position :");
                scanf("%d",&index);
                insertAtAnyPosition(data,index);
                break;
            case 4:
                displayNode();
                break;
            case 5:
                deleteLastNode();
                break;
            case 6:
                exit(0);
            default:
                printf("Invalid choice");
                break;
        }
        getch();
    }


}

Comments

Popular posts from this blog