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.)
Solution
C++ Code for the given problem is below.....
.....................................................
/*
* C++ Program to Implement Singly Linked List
*/
#include
#include
#include
//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\"<>ch;
switch(ch)
{
case 1:
cout<<\"ENTER THE VALUE \"<>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\"<>kth;
s1.delfromkth(&head,kth);
break;
case 4:
cout<<\"ENTER THE Kth Element which you want\'s to search \ \"<>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\ \"<next;
free(temp);
}
else
for(int j=0;jnext;
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 \\t
\"<info<info<next;
i++;
}
}
}
/*
* Traverse Link List
*/
void linkedListType::traverse(node *head)
{
ptr = head;
while((ptr->next)->next!=NULL)
{
cout<info<<\"\\t\";
ptr=ptr->next;
}
cout<
#include
#include
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 .
Please need help on following program using c++ language. Please inc.pdf
1. 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.)
Solution
C++ Code for the given problem is below.....
.....................................................
/*
* C++ Program to Implement Singly Linked List
*/
#include
#include
#include
//using namespace std;
/*
* Node Declaration
*/
typedef struct nodetype
{
int info;
nodetype *next ;
} node;
/*
* Class Declaration
*/
class linkedListType
2. {
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"<>ch;
switch(ch)
{
case 1:
cout<<"ENTER THE VALUE "<>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"<>kth;
s1.delfromkth(&head,kth);
break;
3. case 4:
cout<<"ENTER THE Kth Element which you want's to search "<>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)
4. { node* temp1=*head,*temp2;
if(*head ==NULL)
{
cout<<"LINK LIST IS EMPTY "<next;
free(temp);
}
else
for(int j=0;jnext;
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 t
"<info<info<next;
i++;
}
}
}
/*
* Traverse Link List
*/
void linkedListType::traverse(node *head)
5. {
ptr = head;
while((ptr->next)->next!=NULL)
{
cout<info<<"t";
ptr=ptr->next;
}
cout<
#include
#include
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:
6. 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;
7. *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 %dt",kth,ptr->info);
printf(" ");
break;
}
else
{
printf("%dt",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;jnext;