如何在链表中的某个特定索引处添加节点?
void insertLoc(int n, int i)
在列表中的第 i 个位置之后插入一个包含信息 n 的节点。如果列表中不存在第 i 个位置,则程序应退出并显示错误消息。
谁能帮我看一下代码吗...
#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;
}
void insertLoc(int n, int i)
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅下面的
insertLoc
方法。我更改了size
方法以返回计数,而不是将其打印在屏幕上。列表类不应打印到屏幕上...只需管理列表即可。如果需要在屏幕上打印有关列表的某些内容,最好让main
(或其他一些代码)调用列表类上的方法并打印结果。不要将print
方法作为列表类的一部分,而是将其作为一个独立的函数,接受const list&
或const list*
并进行迭代打印每个节点的列表。See
insertLoc
method below. I changed thesize
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 havemain
(or some other code) call methods on the list class and print the results. Instead of theprint
method being part of the list class, make it a standalone function that takes aconst list&
orconst list*
and iterates the list printing each node.