For MyLinkedList implement addFirst addLast removeFirst remo

For MyLinkedList, implement addFirst, addLast, removeFirst, removeLast, getFirst,
and getLast by making calls to the private add, remove, and getNode routines,
respectively.

Solution

here is my program in C-language to add / remove elements from linked list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
typedef struct node
    {
    int info;
    struct node * next;
    }node;

void insert_position(node *head,int pos_value)
    {
    int ele,flag=1;
    while(head->info!=pos_value)
        {
        head=head->next;
        if(head==NULL)
            {
            flag=2;break;
            }
        }
    if(flag==1)
        {
    printf(\"\ Enter the no to insert= \");
    scanf(\"%d\",&ele);
    node *temp=(node *)malloc(sizeof(node));
    temp->info=ele;
    temp->next=head->next;
    head->next=temp;
        }
    else
        printf(\"\ Value u entered is not in list.\ \");
    }


void sort(node **head)
    {
    node *chk1,*chk2;
    int temp;
    for(chk1=*head;chk1->next!=NULL;chk1=chk1->next)
        {
        for(chk2=*head;chk2->next!=NULL;chk2=chk2->next)
            {
            if(chk2->next->info<chk2->info)
                {
                temp=chk2->next->info;
                chk2->next->info=chk2->info;
                chk2->info=temp;
                }
            }
        }
    }
void insert_at_end(node **head,int n)
    {
    node *temp,*q;
    temp=(node *) malloc(sizeof(node));
    temp->info=n;
    temp->next=NULL;
    if(*head==NULL)
        {
        *head=temp;
        return;
        }
    q=*head;
    while(q->next!=NULL)
        {
        q=q->next;
        }
    q->next=temp;
    }

void display(node *head)
    {
    if(head==NULL)
        {
        printf(\"list is empty\");
        return;
        }
    while(head!=NULL)
        {
        printf(\"%d \",head->info);
        head=head->next;
        }
    }

void insert_at_beg(node **head, int n)
    {
    node *temp;
    temp=(node *)malloc(sizeof(node));
    temp->info=n;
    temp->next=*head;
    *head=temp;
    }


void delete_val(node **head,int n)
    {

    node *q=*head,*temp;
    if(q->info==n)
        {
        temp=q;
        *head=temp->next;
        }
    else
        {
        while(q->next!=NULL)
            {
            if(q->next->info==n)
                {
                temp=q->next;
                q->next=temp->next;
                break;
                }
            q=q->next;
            }
        if(q->next==NULL)
            {
            printf(\"\ No. is not in list.\ \\t!! If it is last node then it has been deleted.!!\");
            return;
            }
        }
        free(temp);
        }

void Reverse_list(node **head)
    {
    node *f,*s,*t;
    f=*head;
    s=f->next;
    t=s->next;
    f->next=NULL;
    while(s!=NULL)
        {
        s->next=f;
        f=s;
        s=t;
        t=t->next;
        }
    *head=f;
    }


void main()
    {
    node *start1=NULL;
    int choice,ele,pos_value,option;
    clrscr();
    do
        {
        printf(\"\ \\t[1] Create\\\\Insert at end\");
        printf(\"\ \\t[2] Create\\\\Insert at Begining\");
        printf(\"\ \\t[3] Sort List\");
        printf(\"\ \\t[4] Delete\");
        printf(\"\ \\t[5] Display\");
        printf(\"\ \\t[6] Reverse List\");
        printf(\"\ \\t[7] Insert at position\");
        printf(\"\ \\t[8] Exit\");
        printf(\"\ \\t\\tEnter ur choice= \");
        scanf(\"%d\",&choice);
        switch(choice)
        {
        case 1:printf(\"\ Enter the element=\");
               scanf(\"%d\",&ele);
               insert_at_end(&start1,ele);
               break;
        case 2:printf(\"\ Enter the element=\");
               scanf(\"%d\",&ele);
               insert_at_beg(&start1,ele);
               break;
        case 3:sort(&start1);
            break;
        case 4:printf(\"\ Enter the element which u want to delete= \");
               scanf(\"%d\",&ele);
               delete_val(&start1,ele);
               break;
        case 5:printf(\"\ Elements in list are:\ \");
               display(start1);
               break;
        case 6:printf(\"\ Reversing link list...:\ \");
               Reverse_list(&start1);
               break;
        case 7:printf(\"\ Enter the element after which u want to insert= \");
               scanf(\"%d\",&pos_value);
               insert_position(start1,pos_value);
               break;
        case 8:break;
        }
    }while(choice!=8);
}

For MyLinkedList, implement addFirst, addLast, removeFirst, removeLast, getFirst, and getLast by making calls to the private add, remove, and getNode routines,
For MyLinkedList, implement addFirst, addLast, removeFirst, removeLast, getFirst, and getLast by making calls to the private add, remove, and getNode routines,
For MyLinkedList, implement addFirst, addLast, removeFirst, removeLast, getFirst, and getLast by making calls to the private add, remove, and getNode routines,
For MyLinkedList, implement addFirst, addLast, removeFirst, removeLast, getFirst, and getLast by making calls to the private add, remove, and getNode routines,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site