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

I have a beginner linked list question. I\'m trying to get ready for a final. I

ID: 3569529 • Letter: I

Question

I have a beginner linked list question. I'm trying to get ready for a final.

I really dont understand. How I can get this function to work. He gives step by step direction of how to write the code, but I learn better if I actually see the code. I have a hard time translating the directions into code. If you could try to fill in the code for the orderedInsert function I would be very thankful.

the supplied code is:

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

             int data;

             struct node *next;

} Node;

Node *orderedInsert(Node *p, int newval);

/* Allocates a new Node with data value newval    and inserts into the ordered list with     first node pointer p in such a way that the    data values in the modified list are in     nondecreasing order as the list is traversed. */

Design of the orderedInsert function:

Here is a step by step description of what you should do to insert an integer newval into an ordered linked list with first node pointer p:

1. Declare a Node pointer variable q, dynamically allocate a Node as the target of q and set the data value of the new node to newval.

2. If the list is empty or newval is less than or equal to the data value in the first node of the list, set the next pointer of q to the first node pointer (p) and return q.

3. Use a Node pointer tmp to traverse the list until it either reaches the last node or reaches a Node whose following node has data value greater than or equal to newval.

4. Insert the target of q after the target of tmp and return p (the original first node pointer).

Explanation / Answer

//Step by Step mention how to create node, add node, delete and traverse linklist.

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
   int data;
   struct node* ptrnextnode;
}Node;

Node* head=NULL;
Node* tail=NULL;

int empty(void)
{
   return head==NULL;
}

Node* create_node(int data)
{
   Node* newnode=(Node*)malloc(sizeof(Node)*1);
   if(newnode!=NULL)
   {
       newnode->data=data;
       newnode->ptrnextnode=NULL;
   }
   return newnode;
}
void add_first(int data)
{
   Node* newnode=create_node(data);
   if(newnode!=NULL)
   {
       if(empty())
       {
           head=newnode;
           tail=newnode;
       }
       else
       {
           newnode->ptrnextnode=head;
           head=newnode;
       }
   }
}
void add_last(int data)
{
   Node* newnode=create_node(data);
   if(newnode!=NULL)
   {
       if(empty())
       {
           head=newnode;
           tail=newnode;
       }
       else
       {
           tail->ptrnextnode=newnode;
           tail=newnode;
       }
   }
}
void accept_data(int* value)
{
   printf(" Enter the value to be added :");
   scanf("%d",value);
}
void print_data(int* value)
{
   printf("%d ",*value);
}
void delete_first()
{
   if(!empty())
   {
   if(head==tail)
   {
       free(head);
       head=tail=NULL;
   }
   else
   {
       Node* temp=head;
       head=head->ptrnextnode;
       free(head);
   }
   }
}
void delete_last()
{
   if(!empty())
   {
       if(head==tail)
       {
           free(head);
           head=tail=NULL;
       }
       else
       {
           Node* trav=head;
           while(trav->ptrnextnode!=tail)
           {
               trav=trav->ptrnextnode;
           }
           free(tail);
           trav=tail;
           trav->ptrnextnode=NULL;
       }
   }
}
void print_list()
{
   if(!empty())
   {
   Node* current_node=head;
   while(current_node!=NULL)
   {
       print_data(&current_node->data);
       current_node=current_node->ptrnextnode;
   }
   printf(" ");
   }
}
int menu_list()
{
   int a;
   printf(" 0.Exit. 1.Add at first. 2.add at last 3.delete at first 4.delete at last. 5.print list of node.");
   printf(" select from the following options :");
   scanf("%d",&a);
   return a;
}
int main( void )
{
   int number;
   int choice;
   while (choice=menu_list()!=0)
   {
       switch(choice)
       {
           case 1:
                   accept_data(&number);
                   add_first(number);
                   break;
           case 2:
                   accept_data(&number);
                   add_last(number);
                   break;
           case 3:
                   delete_first();
                   break;
           case 4:
                   delete_last();
                   break;
           case 5:
                   print_list();
                   break;
   }
}
   return 0;
}