Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C languange: - Implement Selection into a linked list and implement Insertion

ID: 3689929 • Letter: I

Question

In C languange:

- Implement Selection into a linked list and implement Insertion into a linked list.

A template for linked list is provided.

The following functions are provided:

·       fillRand - Fills the linked list with the specified number of non- repeating, random values.

·       print - prints the linked list

·       main - Includes basic code to call the appropriate functions.

Linked List

#include

#include

struct node

{

   int data;

   struct node *next;

};

typedef struct node node;

node* insertBack(node* head, int d)

{

   node *new;

   node *mov;

   new = malloc(sizeof(node));

   new->data = d;

   new->next = NULL;

   if(head == NULL)

       head = new;

   else

   {

       mov = head;

       while(mov->next != NULL)

       {

           mov = mov->next;

       }

       mov->next = new;

   }

   return head;  

}

node* fillRand(int size, int ceil)

{

   int i, tmp;

   int used[size];

   node *front = NULL;

  

   for(i = 0; i < size; i++)

       used[i] = 0;

   for(i = 0; i < size; i++)

   {

       tmp = rand() % ceil;

       while(used[tmp])

           tmp = rand() % ceil;

       used[tmp] = 1;

       front = insertBack(front, tmp);

   }

   return front;

}

void print(node* front)

{

   node* tmp = front;

   while(tmp!=NULL)

   {

       printf("%d ", tmp->data);

       tmp = tmp->next;

   }

   printf(" ");

   return;

}

node* selSort(node* front)

{

   //Your code here!

}

node* insSort(node* front)

{

   //Your code here!

}

int main()

{

   srand(time(0));

   node* front = NULL;

   front = fillRand(10, 10);

   printf("Unsorted list... ");

   print(front);

   //----------------

   //---Choose-------

  

   //front = selSort(front);

   //front = insSort(front);

   //----------------

   printf("Sorted list... ");

   print(front);

   return 0;

}

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

}*head;

void append(int num)

{

    struct node *temp,*right;

    temp= (struct node *)malloc(sizeof(struct node));

    temp->data=num;

    right=(struct node *)head;

    while(right->next != NULL)

    right=right->next;

    right->next =temp;

    right=temp;

    right->next=NULL;

}

void add( int num )

{

    struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=num;

    if (head== NULL)

    {

    head=temp;

    head->next=NULL;

    }

    else

    {

    temp->next=head;

    head=temp;

    }

}

void addafter(int num, int loc)

{

    int i;

    struct node *temp,*left,*right;

    right=head;

    for(i=1;i<loc;i++)

    {

    left=right;

    right=right->next;

    }

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=num;

    left->next=temp;

    left=temp;

    left->next=right;

    return;

}

void insert(int num)

{

    int c=0;

    struct node *temp;

    temp=head;

    if(temp==NULL)

    {

    add(num);

    }

    else

    {

    while(temp!=NULL)

    {

        if(temp->data<num)

        c++;

        temp=temp->next;

    }

    if(c==0)

        add(num);

    else if(c<count())

        addafter(num,++c);

    else

        append(num);

    }

}

int delete(int num)

{

    struct node *temp, *prev;

    temp=head;

    while(temp!=NULL)

    {

    if(temp->data==num)

    {

        if(temp==head)

        {

        head=temp->next;

        free(temp);

        return 1;

        }

        else

        {

        prev->next=temp->next;

        free(temp);

        return 1;

        }

    }

    else

    {

        prev=temp;

        temp= temp->next;

    }

    }

    return 0;

}

void display(struct node *r)

{

    r=head;

    if(r==NULL)

    {

    return;

    }

    while(r!=NULL)

    {

    printf("%d ",r->data);

    r=r->next;

    }

    printf(" ");

}

int count()

{

    struct node *n;

    int c=0;

    n=head;

    while(n!=NULL)

    {

    n=n->next;

    c++;

    }

    return c;

}

int main()

{

    int i,num;

    struct node *n;

    head=NULL;

    while(1)

    {

    printf(" List Operations ");

    printf("=============== ");

    printf("1.Insert ");

    printf("2.Display ");

    printf("3.Size ");

    printf("4.Delete ");

    printf("5.Exit ");

    printf("Enter your choice : ");

    if(scanf("%d",&i)<=0){

        printf("Enter only an Integer ");

        exit(0);

    } else {

        switch(i)

        {

        case 1:      printf("Enter the number to insert : ");

                 scanf("%d",&num);

                 insert(num);

                 break;

        case 2:     if(head==NULL)

                {

                printf("List is Empty ");

                }

                else

                {

                printf("Element(s) in the list are : ");

                }

                display(n);

                break;

        case 3:     printf("Size of the list is %d ",count());

                break;

        case 4:     if(head==NULL)

                printf("List is Empty ");

                else{

                printf("Enter the number to delete : ");

                scanf("%d",&num);

                if(delete(num))

                    printf("%d deleted successfully ",num);

                else

                    printf("%d not found in the list ",num);

                }

                break;

        case 5:     return 0;

        default:    printf("Invalid option ");

        }

    }

    }

    return 0;

}#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

}*head;

void append(int num)

{

    struct node *temp,*right;

    temp= (struct node *)malloc(sizeof(struct node));

    temp->data=num;

    right=(struct node *)head;

    while(right->next != NULL)

    right=right->next;

    right->next =temp;

    right=temp;

    right->next=NULL;

}

void add( int num )

{

    struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=num;

    if (head== NULL)

    {

    head=temp;

    head->next=NULL;

    }

    else

    {

    temp->next=head;

    head=temp;

    }

}

void addafter(int num, int loc)

{

    int i;

    struct node *temp,*left,*right;

    right=head;

    for(i=1;i<loc;i++)

    {

    left=right;

    right=right->next;

    }

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=num;

    left->next=temp;

    left=temp;

    left->next=right;

    return;

}

void insert(int num)

{

    int c=0;

    struct node *temp;

    temp=head;

    if(temp==NULL)

    {

    add(num);

    }

    else

    {

    while(temp!=NULL)

    {

        if(temp->data<num)

        c++;

        temp=temp->next;

    }

    if(c==0)

        add(num);

    else if(c<count())

        addafter(num,++c);

    else

        append(num);

    }

}

int delete(int num)

{

    struct node *temp, *prev;

    temp=head;

    while(temp!=NULL)

    {

    if(temp->data==num)

    {

        if(temp==head)

        {

        head=temp->next;

        free(temp);

        return 1;

        }

        else

        {

        prev->next=temp->next;

        free(temp);

        return 1;

        }

    }

    else

    {

        prev=temp;

        temp= temp->next;

    }

    }

    return 0;

}

void display(struct node *r)

{

    r=head;

    if(r==NULL)

    {

    return;

    }

    while(r!=NULL)

    {

    printf("%d ",r->data);

    r=r->next;

    }

    printf(" ");

}

int count()

{

    struct node *n;

    int c=0;

    n=head;

    while(n!=NULL)

    {

    n=n->next;

    c++;

    }

    return c;

}

int main()

{

    int i,num;

    struct node *n;

    head=NULL;

    while(1)

    {

    printf(" List Operations ");

    printf("=============== ");

    printf("1.Insert ");

    printf("2.Display ");

    printf("3.Size ");

    printf("4.Delete ");

    printf("5.Exit ");

    printf("Enter your choice : ");

    if(scanf("%d",&i)<=0){

        printf("Enter only an Integer ");

        exit(0);

    } else {

        switch(i)

        {

        case 1:      printf("Enter the number to insert : ");

                 scanf("%d",&num);

                 insert(num);

                 break;

        case 2:     if(head==NULL)

                {

                printf("List is Empty ");

                }

                else

                {

                printf("Element(s) in the list are : ");

                }

                display(n);

                break;

        case 3:     printf("Size of the list is %d ",count());

                break;

        case 4:     if(head==NULL)

                printf("List is Empty ");

                else{

                printf("Enter the number to delete : ");

                scanf("%d",&num);

                if(delete(num))

                    printf("%d deleted successfully ",num);

                else

                    printf("%d not found in the list ",num);

                }

                break;

        case 5:     return 0;

        default:    printf("Invalid option ");

        }

    }

    }

    return 0;

}#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

}*head;

void append(int num)

{

    struct node *temp,*right;

    temp= (struct node *)malloc(sizeof(struct node));

    temp->data=num;

    right=(struct node *)head;

    while(right->next != NULL)

    right=right->next;

    right->next =temp;

    right=temp;

    right->next=NULL;

}

void add( int num )

{

    struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=num;

    if (head== NULL)

    {

    head=temp;

    head->next=NULL;

    }

    else

    {

    temp->next=head;

    head=temp;

    }

}

void addafter(int num, int loc)

{

    int i;

    struct node *temp,*left,*right;

    right=head;

    for(i=1;i<loc;i++)

    {

    left=right;

    right=right->next;

    }

    temp=(struct node *)malloc(sizeof(struct node));

    temp->data=num;

    left->next=temp;

    left=temp;

    left->next=right;

    return;

}

void insert(int num)

{

    int c=0;

    struct node *temp;

    temp=head;

    if(temp==NULL)

    {

    add(num);

    }

    else

    {

    while(temp!=NULL)

    {

        if(temp->data<num)

        c++;

        temp=temp->next;

    }

    if(c==0)

        add(num);

    else if(c<count())

        addafter(num,++c);

    else

        append(num);

    }

}

int delete(int num)

{

    struct node *temp, *prev;

    temp=head;

    while(temp!=NULL)

    {

    if(temp->data==num)

    {

        if(temp==head)

        {

        head=temp->next;

        free(temp);

        return 1;

        }

        else

        {

        prev->next=temp->next;

        free(temp);

        return 1;

        }

    }

    else

    {

        prev=temp;

        temp= temp->next;

    }

    }

    return 0;

}

void display(struct node *r)

{

    r=head;

    if(r==NULL)

    {

    return;

    }

    while(r!=NULL)

    {

    printf("%d ",r->data);

    r=r->next;

    }

    printf(" ");

}

int count()

{

    struct node *n;

    int c=0;

    n=head;

    while(n!=NULL)

    {

    n=n->next;

    c++;

    }

    return c;

}

int main()

{

    int i,num;

    struct node *n;

    head=NULL;

    while(1)

    {

    printf(" List Operations ");

    printf("=============== ");

    printf("1.Insert ");

    printf("2.Display ");

    printf("3.Size ");

    printf("4.Delete ");

    printf("5.Exit ");

    printf("Enter your choice : ");

    if(scanf("%d",&i)<=0){

        printf("Enter only an Integer ");

        exit(0);

    } else {

        switch(i)

        {

        case 1:      printf("Enter the number to insert : ");

                 scanf("%d",&num);

                 insert(num);

                 break;

        case 2:     if(head==NULL)

                {

                printf("List is Empty ");

                }

                else

                {

                printf("Element(s) in the list are : ");

                }

                display(n);

                break;

        case 3:     printf("Size of the list is %d ",count());

                break;

        case 4:     if(head==NULL)

                printf("List is Empty ");

                else{

                printf("Enter the number to delete : ");

                scanf("%d",&num);

                if(delete(num))

                    printf("%d deleted successfully ",num);

                else

                    printf("%d not found in the list ",num);

                }

                break;

        case 5:     return 0;

        default:    printf("Invalid option ");

        }

    }

    }

    return 0;

}