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

#include <stdio.h> #include <iostream.h> #include <conio.h> #include <stdlib.h>

ID: 3874989 • Letter: #

Question

#include <stdio.h>

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

struct stock

{

int data;

struct stock *next;

}*nnode,*ptr,*p,*prev,*start;

class LinkedList

{

public:

LinkedList()

{

start=NULL;

}

void create()

{

nnode=new stock;

cout<<"enter the data you want to enter";

cin>>nnode->data;

nnode->next=NULL;

start=nnode;

}

void addAtBeginning()

{

nnode=new stock;

cout<<"enter the data you want to enter";

cin>>nnode->data;

nnode->next=start;

start=nnode;

}

void addAtLast()

{

for(ptr=start;ptr!=NULL;prev=ptr,ptr=ptr->next);

cout<<"enter the data you want to enter";

nnode=new stock;

cin>>ptr->data;

prev->next=nnode;

nnode->next=NULL;

}

void deleteFirst()

{

ptr=start;

start=ptr->next;

delete(ptr);

}

void deleteLast()

{

for(ptr=start;ptr!=NULL;p=prev,prev=ptr,ptr=ptr->next);

p->next=NULL;

delete(prev);

}

void display()

{

for(ptr=start;ptr!=NULL;ptr=ptr->next)

cout<<ptr->data;

}

void addAtParticularPosition()

{


cout<<"Enter the location at which you want the node to inserted";
cin>>location;
if(location>totalNumberOfNodes() ||
location<0)
{
cout<<"Invalid location";
else

}

}

void deleteAtParticularPositon()

{

cout<<"Enter the location at which you want the node to inserted";
cin>>location;
if(location>totalNumberOfNodes() ||
location<0)
{
cout<<"Invalid location";
else

}

}

};

int main()

{

int ch;

LinkedList l;

clrscr();

while(1)

{

cout<<"enter choice";

cin>>ch;

switch(ch)

{

case 1:l.create();

break;

case 2:l.addAtBeginning();

break;

case 3:l.addAtLast();

break;

case 4:l.deleteFirst();

break;

case 5:l.deleteLast();

break;

case 6:l.display();

break;

case 7:l.totalNumberOfNodes();

break;

case 8: exit(1);

case 9:l.addAtParticularPosition();

break;

case 10:l.deleteAtParticularPositon();

break;

default:cout<<"invalid choice";

}

}

getch();

return 0;

}

Please complete the void addAtParticularPosition() function to add any node at any desired position considering it should add first node or last node or any node in the middle which user enters and void deleteAtParticularPositon() to delete any node at any desired position which should be able to delete first node or last node or any node in middle keeping the entire structure same.

Explanation / Answer

The completed functions are as follows:


int totalNumberOfNodes(){

    stock *ptr;
    ptr = start;
    int count = 0;
    while (ptr != NULL){
        count++;
        ptr = ptr->next;
    }
    return count;
}

void addAtParticularPosition()
{
int data;
int location;
cout<<"Enter the location at which you want the node to be inserted";
cin>>location;
if(location>totalNumberOfNodes() || location<0) // The first node is at index 0
{
cout<<"Invalid location";
}
else {
    stock nnode = new stock;
    cout << "Enter the data you want to enter:";
    cin >> nnode->data;
    nnode->next = NULL;
   
    stock *ptr;
    int count = 0;
    ptr = start;
    if (location == 0){
         nnode->next = start;
         start - nnode;
    }
    else {
      count = 0;
      while(count != location-1){
        ptr = ptr->next;
        count++;
      }
      nnode->next = ptr->next;
      ptr->next = nnode;
    }   
}
}


void deleteAtParticularPositon()
{
int location;
cout<<"Enter the location which you want to delete";
cin>>location;
if(location>totalNumberOfNodes()-1 || location<0) // The first node is at index 0
{
cout<<"Invalid location";
}
else {
    stock *ptr,*q;
    int count = 0;
    ptr = start;
    if (location == 0){
       ptr = start;
       start = start->next;
       delete(ptr);
    }
    else {
      count = 0;
      while(count != location-1){
        ptr = ptr->next;
        count++;
      }
      q = ptr->next;
      ptr->next = q->next;
      delete(q);
    }   
}

}