如何在链表中的某个特定索引处添加节点?

发布于 2024-09-27 13:38:50 字数 2470 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-10-04 13:38:50

请参阅下面的 insertLoc 方法。我更改了 size 方法以返回计数,而不是将其打印在屏幕上。列表类不应打印到屏幕上...只需管理列表即可。如果需要在屏幕上打印有关列表的某些内容,最好让 main (或其他一些代码)调用列表类上的方法并打印结果。不要将 print 方法作为列表类的一部分,而是将其作为一个独立的函数,接受 const list&const list* 并进行迭代打印每个节点的列表。

#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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文