申明一个链表然后把一个变量插到任意一个位置。。请高手指教。不要用MFC.c++的语法就可以

解决方案 »

  1.   

    给你看段STL例子:
    #include "stdafx.h" #include <list>
     #include <string>
     #include <iostream>
     #include <algorithm> using namespace std;
     // Print out a list of strings
     ostream& operator<<(ostream& out, const list<string>& l)
     {
       copy(l.begin(), l.end(), 
            ostream_iterator<string,char>(cout," "));
       return out;
     } int main(void)
     {
       // create a list of critters
       list<string> critters;
       int i;
       // insert several critters 
       critters.insert(critters.begin(),"antelope");
       critters.insert(critters.begin(),"bear");   critters.insert(critters.begin(),"cat");
       // print out the list
       cout << critters << endl;
       
       // Change cat to cougar
       *find(critters.begin(),critters.end(),"cat") = "cougar";
       cout << critters << endl;
       // put a zebra at the beginning 
       // an ocelot ahead of antelope
       // and a rat at the end
       critters.push_front("zebra");
       critters.insert(find(critters.begin(),critters.end(),
                       "antelope"),"ocelot");
       critters.push_back("rat");   cout << critters << endl;
       // sort the list (Use list's sort function since the 
       // generic algorithm requires a random access iterator 
       // and list only provides bidirectional)
       critters.sort();
       cout << critters << endl;
       // now let's erase half of the critters
       int half = critters.size() >> 1;
       for(i = 0; i < half; ++i) {
         critters.erase(critters.begin());
       }
       cout << critters << endl;
       return 0;
     }
      

  2.   

    应该可以好好的看看数据结构。插入任意位置 其实只有三种位置呀
    最前面 中间 最后面
     ...n-1 n n+1 ...
     ...n-1 A n+1 ...
    在n位置插入 A ,则需要 处理 原 n-1 与 n的关系重新处理为
    n-1 -> next = A;
    A->previous = n-1;
    A->next = n
    n->previous = A.差不多就这样.