How to add node at some specific index in a linked list?
void insertLoc(int n, int i)
开发者_Go百科inserts a node with info n after the ith location in the list. If the ith location does not exist in the list then the program should exit with an error message.
Can anyone help me with the code please...
#include<iostream>
#include<process.h>
using namespace std;
struct node {
int info;
node *nextptr;
};
class list {
node *L,*tail;
int count;
public:
list() {
L=tail=NULL;
count=0;
}
void size();
void InsertHead(int info);
int RemoveHead();
void Print();
void insertLoc(int n, int i);
};
void list::size() {
cout<<"size is :"<<count<<endl;
}
void list::Print() {
node *p;
p=L;
cout<<"\n\n";
while(p!=NULL) {
cout<<p->info<<"\t";
p=p->nextptr;
}
}
int list::RemoveHead() {
int RemoveNode;
if(L==NULL) {
cout<<"\n\nSTACK EMPTY\n\n";
exit(1);
}
node *temp;
temp=L;
RemoveNode=L->info;
L=L->nextptr;
delete temp;
--count;
return RemoveNode;
}
void list::InsertHead(int info) {
node *n=new node;
n->info=info;
n->nextptr=L;
L=n;
++count;
}
int main() {
int choice,info;
list L;
while(1) {
cout<<"\nENTER 1 FOR INSERT\n";
cout<<"ENTER 2 FOR PRINT \n";
cout<<"ENTER 3 FOR REMOVE\n";
cout<<"ENTER 4 FOR SIZE\n";
cout<<"ENTER 5 FOR SPECIFIC LOCATON\n";
cout<<"ENTER 6 FOR EXIT\n\n";
cin>>choice;
if(choice==1) {
cout<<"\n\nENTER VALUE FOR PUSH=\t";
cin>>info;
L.InsertHead(info);
} else
if(choice==2) {
L.Print();
} else
if(choice==3) {
cout<<"REMOVE ITEM=\t"<<L.RemoveHead()<<endl;
} else
if(choice==4)
{
L.size();
}
else
if(choice==5)
{
int infoo,pos;
cout<<"Enter info value nd pos=\t";
cin>>infoo;
cin>>pos;
L.insertLoc(infoo, pos);
}
else
{
exit();
}
}
return 0;
}
See insertLoc
method below. I changed the size
method to return count instead of printing it on the screen. A list class shouldn't print to the screen...just manage the list. If something about the list needs to be printed on the screen, it would be better to have main
(or some other code) call methods on the list class and print the results. Instead of the print
method being part of the list class, make it a standalone function that takes a const list&
or const list*
and iterates the list printing each node.
#include <iostream>
#include <process.h>
using namespace std;
struct node {
int info;
node *nextptr;
};
class list {
node *L,*tail;
int count;
public:
list() {
L=tail=NULL;
count=0;
}
int size();
void InsertHead(int info);
int RemoveHead();
void Print();
bool insertLoc(int n, int i);
};
bool list::insertLoc(int n, int i) {
if (i > count)
return false; // error
node *p = L;
while (i) {
p = p->nextptr;
--i;
}
node *z = new node;
z->info = n;
z->nextptr = p->nextptr;
p->nextptr = z;
++count;
return true;
}
int list::size() {
return count;
}
void list::Print() {
node *p;
p=L;
cout<<"\n\n";
while(p!=NULL) {
cout<<p->info<<"\t";
p=p->nextptr;
}
}
int list::RemoveHead() {
int RemoveNode;
if(L==NULL) {
cout<<"\n\nSTACK EMPTY\n\n";
exit(1);
}
node *temp;
temp=L;
RemoveNode=L->info;
L=L->nextptr;
delete temp;
--count;
return RemoveNode;
}
void list::InsertHead(int info) {
node *n=new node;
n->info=info;
n->nextptr=L;
L=n;
++count;
}
int main() {
int choice,info;
list L;
while(1) {
cout<<"\nENTER 1 FOR INSERT\n";
cout<<"ENTER 2 FOR PRINT \n";
cout<<"ENTER 3 FOR REMOVE\n";
cout<<"ENTER 4 FOR SIZE\n";
cout<<"ENTER 5 FOR SPECIFIC LOCATON\n";
cout<<"ENTER 6 FOR EXIT\n\n";
cin>>choice;
if(choice==1) {
cout<<"\n\nENTER VALUE FOR PUSH=\t";
cin>>info;
L.InsertHead(info);
} else
if(choice==2) {
L.Print();
} else
if(choice==3) {
cout<<"REMOVE ITEM=\t"<<L.RemoveHead()<<endl;
} else
if(choice==4)
{
cout << "size is :" << L.size() << endl;
}
else
if(choice==5)
{
int infoo,pos;
cout<<"Enter info value nd pos=\t";
cin>>infoo;
cin>>pos;
if (!L.insertLoc(infoo, pos)) {
cout << "insertLoc(" << infoo << ", " << pos
<< ") failed. List size=" << L.size() << endl;
break;
}
}
else
{
exit(1);
}
}
return 0;
}
精彩评论