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

Please need help on following program using c++ language. Please include all hea

ID: 3936887 • Letter: P

Question

Please need help on following program using c++ language. Please include all header files and main file with their own title.

Extend the class linkedListType by adding the following operations:

Write a function that returns the info of the kth element of the linked list. If no such element exists, terminate the program.

Write a function that deletes the kth element of the linked list. If no such element exists, terminate the program.

Provide the definitions of these functions in the class  linkedListType. Also, write a program to test these functions. (Use either the class unorderedLinkedList  or the class  orderedLinkedList  to test your function.)

Explanation / Answer

C++ Code for the given problem is below.....

.....................................................

/*
* C++ Program to Implement Singly Linked List
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
/*
* Node Declaration
*/
typedef struct nodetype
   {
       int info;
       nodetype *next ;
   } node;

/*
* Class Declaration
*/
class linkedListType
{
public:
void insfrombeg( node **head, int);
   void delfromkth(node **head,int);
   void findthekthnode(node *head,int);
   void traverse(node *head);   
   int item,ch,kth;
   node *ptr,*head,*temp;

};

/*
* Main :contains menu
*/
void main()
{
int item,ch,kth;
node *ptr,*head,*temp;
linkedListType s1;
clrscr();
while (1)
{
       while(1)
       {
       cout<<"PRESS (1) FOR INSERT INTO LIST"<<endl;
       cout<<"PRESS (2) FOR TRAVERSE FROM INORDER"<<endl;
       cout<<"PRESS (3) FOR DELETE FROM kth NODE"<<endl;
       cout<<"PRESS (4) FOR FIND THE Kth NODE"<<endl;
       cout<<"PRESS (5) FOR EXIT FROM MENU"<<endl;
       cout<<"ENTER YOUR CHOICE"<<endl;
       cin>>ch;
           switch(ch)
           {
               case 1:
               cout<<"ENTER THE VALUE "<<endl;
               cin>>item;
               s1.insfrombeg(&head,item);
               break;
               case 2:
               s1.traverse(head);
               break;
               case 3:
               cout<<"ENTER THE Kth Element which you want's to delete"<<endl;
               cin>>kth;
               s1.delfromkth(&head,kth);
               break;
               case 4:
               cout<<"ENTER THE Kth Element which you want's to search "<<endl;
               cin>>kth;
               s1.findthekthnode(head,kth);
               break;
               case 5:
               exit(1);
           }
       }

   //   getch();
   }
}
/*
* Inserting element in beginning
*/
void linkedListType::insfrombeg(node **head,int item)
               {
                   temp= ( node * ) malloc (sizeof( node ));
                   temp->info= item;
                   temp->next= NULL;
                   if(*head == NULL)
                   {
                   temp->next= *head;
                   *head= temp;
                   }
                   else
                   {
                   temp->next= *head;
                   *head= temp;
                   }
               }


/*
* Delete element at a given position
*/
void linkedListType::delfromkth(node **head,int kth)
               { node* temp1=*head,*temp2;
                   if(*head ==NULL)
                   {
                       cout<<"LINK LIST IS EMPTY "<<endl;
                       exit;
                   }
                   if(kth==1)
                   {
               *head= temp1->next;
                   free(temp);
                   }
                   else
                   for(int j=0;j<kth-2;j++)
                   temp1=temp1->next;
                   temp2= temp1->next;
                   temp1->next=temp2->next;
                   free(temp2);
               }


/*
* Searching an element
*/
void linkedListType::findthekthnode(node *head,int kth)
               {   
                       ptr=head;
                       int i=1;
                       while((ptr!=NULL))
                       {
                       if(i==kth)
                       {
                       cout<<" The Element at"<< kth<< "position with value is "<<ptr->info<<endl;
                       cout<<endl;
                       break;
                       }
                       else
                       {
                       cout<<ptr->info<<endl;
                       ptr=ptr->next;
                       i++;
                       }
                       }

               }

/*
* Traverse Link List
*/
void linkedListType::traverse(node *head)
               {
                   ptr = head;
                   while((ptr->next)->next!=NULL)
                   {
                       cout<<ptr->info<<" ";
                       ptr=ptr->next;
                   }
               cout<<endl;
               }

...........................................................................................................

please write me if you have any problem regardin this.....

..........................................................................................................

C Code for the given problem ..

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
   typedef struct nodetype
   {
       int info;

nodetype *next ;

   } node;
   int item,ch,kth,total=1;
   node *ptr,*head,*temp;

   void insfrombeg( node **head, int);
   void delfromkth(node **head,int);
   void findthekthnode(node *head,int);
   void traverse(node *head);
void main()
   {
   clrscr();
       while(1)
       {
       printf("PRESS (1) FOR INSERT INTO LIST ");
       printf("PRESS (2) FOR TRAVERSE FROM INORDER ");
       printf("PRESS (3) FOR DELETE FROM kth NODE " );
       printf("PRESS (4) FOR FIND THE Kth NODE " );
       printf("PRESS (5) FOR EXIT FROM MENU ");
       printf("ENTER YOUR CHOICE ");
       scanf("%d",&ch);
           switch(ch)
           {
               case 1:
               printf("ENTER THE VALUE ") ;
               scanf("%d",&item);
               insfrombeg(&head,item);
               break;
               case 2:
               traverse(head);
               break;
               case 3:
               printf("ENTER THE Kth Element which you want's to delete ") ;
               scanf("%d",&kth);
               delfromkth(&head,kth);
               break;
               case 4:
               printf("ENTER THE Kth Element which you want's to search ");
               scanf("%d",&kth);
               findthekthnode(head,kth);
               break;
               case 5:
               exit(1);
           }
       }

   //   getch();
   }
               void insfrombeg(node **head,int item)
               {
                   temp= ( node * ) malloc (sizeof( node ));
                   temp->info= item;
                   temp->next= NULL;
                   if(*head == NULL)
                   {
                   temp->next= *head;
                   *head= temp;
                   }
                   else
                   {
                   temp->next= *head;
                   *head= temp;
                   }
               }
               void findthekthnode(node *head,int kth)
               { ptr=head;
                       int i=1;
                       while((ptr!=NULL))
                       {
                       if(i==kth)
                       {
                       printf(" The Element at %d position with value is %d ",kth,ptr->info);
                       printf(" ");
                       break;
                       }
                       else
                       {
                       printf("%d ",ptr->info);
                       ptr=ptr->next;
                       i++;
                       }
                       }

               }
               void delfromkth(node **head,int kth)
               { node* temp1=*head,*temp2;
                   if(*head ==NULL)
                   {
                       printf("LINK LIST IS EMPTY ");
                       exit;
                   }
                   if(kth==1)
                   {
               *head= temp1->next;
                   free(temp);
                   }
                   else
                   for(int j=0;j<kth-2;j++)
                   temp1=temp1->next;
                   temp2= temp1->next;
                   temp1->next=temp2->next;
                   free(temp2);
               }
               void traverse(node *head)
               {
                   ptr = head;
                   while(ptr!=NULL)
                   {
                       printf("%d ",ptr->info);
                       ptr=ptr->next;
                   }
                   printf(" ");
               }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote