Quick and easy points!! Very Short Program! create a code where the insert and r
ID: 3561767 • Letter: Q
Question
Quick and easy points!! Very Short Program!
create a code where the insert and remove function is changed to where the input parameter from a given index to the data value in from of the processed item. The code should also include the error checking on the given value, if the value is not in the list, the program will show an error message.
#include
#include
using namespace std;
char name[20];
class node
{
public:
int data;
node* next;
};
class PointerList
{
public:
PointerList()
{
top = NULL;
count=0;
}
bool empty()
{
if(count==0)
return true;
else
return false;
}
void insert(int after_position, int element)
{
node *newelement = new node;
if(top == NULL)
{
newelement->data = element;
newelement->next = NULL;
top = newelement;
count++;
}
else
{
node *temp;
temp = top;
for(int i=0;i
temp=temp->next;
newelement->data = element;
newelement->next = temp->next;
temp->next = newelement;
count++;
}
}
void remove(int after_position)
{
node *temp;
temp = top;
for(int i=0;i
temp=temp->next;
node * old = temp->next;
temp->next = old->next;
count--;
delete(old);
}
void print()
{
node *temp;
temp = top;
while(temp!=NULL)
{
cout<data<<",";
temp=temp->next;
}
}
private:
node *top;
int count; //head
int stackData;
};
int main() {
PointerList *sl = new PointerList();
sl->insert(0,10);
sl->insert(0,20);
sl->insert(1,30);
sl->insert(1,40);
sl->insert(0,50);
sl->insert(3,60);
sl->insert(5,70);
sl->remove(3);
sl->print();
cin>>name;
return 0;
}
Explanation / Answer
If I am not wrong you may perhaps test this code(linked list) and see :
This code is working fine without any error...
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*first=NULL;
void insert()
{
struct node *temp;
struct node *nn=(struct node*)malloc(sizeof(struct node));
printf("enter the data ");
scanf("%d",&nn->data);
if(first!=NULL){
temp=first;
while(temp->next!=first)
temp=temp->next;
temp->next=nn;
}else{
first=nn;
}
nn->next=NULL;
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("no elements ");
return;
}
printf("elements in linked list are ");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void deletion()
{
struct node *temp;
temp=first;
first=first->next;
temp->next=NULL;
free(temp);
}
int main()
{
int op;
do
{
printf("1.insertion 2.deletion 3.display 4.exi ");
printf("enter option ");
scanf("%d",&op);
switch(op)
{
case 1:insert();
break;
case 2:deletion();
break;
case 3:display();
break;
}
}while(op!=6);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.