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

时间:2010-10-16 08:29:43

标签: c++ data-structures linked-list

void insertLoc(int n,int i)

在列表中的第i个位置之后插入带有info 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;
}

1 个答案:

答案 0 :(得分:0)

请参阅下面的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;
}