Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 拆分链接列表_C++_List_Hyperlink - Fatal编程技术网

C++ 拆分链接列表

C++ 拆分链接列表,c++,list,hyperlink,C++,List,Hyperlink,我试图将int的链表拆分为一个特定值,其值大于或小于自己列表中的值。我的代码当前在尝试ptr2->data=original->data时出现seg故障 这是我的密码: #include<iostream> #include<cstdlib> #include<ctime> using namespace std; struct node{ int data; node * link; }; void build_list(node* &

我试图将int的链表拆分为一个特定值,其值大于或小于自己列表中的值。我的代码当前在尝试ptr2->data=original->data时出现seg故障

这是我的密码:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

struct node{
 int data;
 node * link;
};



void build_list(node* & head);  
void show_list(const node* head);
int size(const node* head);


 void remove_repeats(node*& head);
 void split_list(const node* original, node*& lesser, 
    node*& greater,int split_value);


int main(){
int start, stop;
int split;
node* head = NULL;
node *lesser;
node * greater;

start = time(NULL);
build_list(head);
stop = time(NULL);
cout<<"Time to build list = "<<stop - start <<"seconds.\n";
start = time(NULL);
show_list(head);
stop = time(NULL);
cout<<"Time to print list = "<<stop - start<<"seconds.\n";
cout<<"Size of the list = "<<size(head)<<endl;
cout<<"Removing repeats" << endl;
remove_repeats(head);
show_list(head);
cout<<"The size of the list is now " << size(head) << endl;
cout<<"Enter value to split list at" << endl;
cin >> split;
split_list(head, lesser, greater, split);
cout<<"The number less than split value" << endl;
show_list(lesser);
cout<<"The numbers greater than splt value" << endl;
show_list(greater);

return 0;
}

// builds a linked list of 2000 random integers, all in the 1 to 500 range
void build_list(node*& head){
 node* cursor;
 head = new node;
 head->data = rand()%500 + 1;
 cursor = head;
 for(int i = 0; i < 2000; ++i){
cursor->link = new node;
    cursor = cursor->link;
    cursor->data = rand()%500 + 1;
 }
 cursor->link = NULL;
}

// outputs the contents of a linked list to the screen
void show_list(const node* head){
  const node * cursor = head;
  while(cursor !=  NULL){
cout<<cursor->data<<"  ";
cursor = cursor->link;
  }
 cout<<endl;
}

// returns the number of nodes in a linked list
int size(const node* head){
const node* cursor = head;
int count = 0;
while(cursor != NULL){
    count++;
    cursor = cursor->link;
}
return count;
}

void remove_repeats(node*& head)
{
node *cur, *next, *prev;

cur = head;

while(cur != NULL){
       next = cur->link;
       prev = cur;
            while(next != NULL){
               if(next->data == cur->data){
                  prev->link = next->link;
                  delete next;
                  next = prev->link;
               }
               else{
                  prev = next;
                  next = next->link;
               }
            }
       cur = cur->link;
    }
}

void split_list(const node* original, node*& lesser, node*& greater, int split_value){

    node* ptr1;
    node* ptr2;
    ptr1 = lesser;
    ptr2 = greater;

        for(original; original != NULL; original = original->link){

                if(original->data < split_value){
           ptr1->data = original->data;
           ptr1->link = new node;
           ptr1 = ptr1->link;
                }
        else if(original->data > split_value){
               ptr2->data = original->data;
                   ptr2->link = new node;
                   ptr2 = ptr2->link;

                }
    }
}
#包括
#包括
#包括
使用名称空间std;
结构节点{
int数据;
节点*链接;
};
无效构建列表(节点*&头部);
无效显示列表(常量节点*头);
整数大小(常数节点*头);
void remove_重复(节点*&头部);
无效拆分列表(常量节点*原始、节点*&较小、,
节点*&更大,整数分割值);
int main(){
int启动、停止;
整数分割;
node*head=NULL;
节点*较小;
节点*更大;
开始=时间(空);
编制清单(头);
停止=时间(空);

cout您的函数会出现故障,因为您正在取消对空指针或不确定指针的引用,并且很容易看到以下位置:

如果要通过引用传递两个指针,以便最终填充上下列表,则:

void split_list(const node* original, node*& lesser, node*& greater, int split_value){

    node* ptr1;
    node* ptr2;
    ptr1 = lesser;  // lesser is NULL or indeterminate, so is ptr1
    ptr2 = greater; // greater is NULL or indeterminate, so is ptr2

    for(original; original != NULL; original = original->link){

        if(original->data < split_value){
            ptr1->data = original->data; // ptr1 still bad. boom
            ptr1->link = new node;
            ptr1 = ptr1->link;
        }
        else if(original->data > split_value){
            ptr2->data = original->data; // ptr2 still bad, boom
            ptr2->link = new node;
            ptr2 = ptr2->link;
        }
    }
}
即不确定。在调用拆分函数之前从未更改

我认为你对这个作业读得太多了。正如你所说的,没有必要复制原始列表的副本。也只需通过引用传入头指针,然后从该列表中提取节点,分别填充较低和较高的列表。下面的代码就是这样做的一种方法,使用技术调用使用指向指针的指针的正向链接,该指针始终包含存储列表下一个节点指针的地址:

void split_list(node *& head, node *& lhs, node *& rhs, int value)
{
    node **pplhs = &lhs;
    node **pprhs = &rhs;

    for (;head; head = head->link)
    {
        node **& pp = (head->data < value) ? pplhs : pprhs;
        *pp = head;
        pp = &(*pp)->link;
    }
    *pplhs = *pprhs = nullptr;
}
void split_列表(节点*&头部,节点*&左侧,节点*&右侧,int值)
{
节点**pplhs=&lhs;
节点**pprhs=&rhs;
用于(;头部;头部=头部->链接)
{
节点**&pp=(头部->数据<值)?pplhs:PPRH;
*pp=头部;
pp=&(*pp)->链接;
}
*pplhs=*pprhs=nullptr;
}
完成后,原始列表为空(
head
将为空),并且根据拆分值测试设置了另外两个列表。未创建新节点;节点取自原始列表,保留为空


祝你好运

任何时候当你谈论拆分或合并现有的链接列表时。忘记(a)分配新节点,或(b)复制成员数据。链表算法是关于指针管理的。花在铅笔末端的大量时间在一张纸上画框和箭头最终会使其凝固。简言之,除非复制列表和spli,否则拆分函数中应该不需要使用
新节点;
数据=…
我先创建一个包含2000个整数的列表,然后删除所有重复的整数。接下来,我输入一个值,将缩减后的列表拆分为两个其他列表,一个值小于拆分值,另一个值大于拆分值。是否可以在创建列表时对其进行排序。这将使du复制删除和拆分琐碎。这是有意义的。但是,只删除重复项然后拆分列表是一种分配。@sa044512您是否分别测试了删除重复值以确保该例程中没有错误。我的意思是没有错误?例如,一个简单的main()删除重复项后成功打印列表的程序?如果没有,则需要先执行此操作。
void split_list(node *& head, node *& lhs, node *& rhs, int value)
{
    node **pplhs = &lhs;
    node **pprhs = &rhs;

    for (;head; head = head->link)
    {
        node **& pp = (head->data < value) ? pplhs : pprhs;
        *pp = head;
        pp = &(*pp)->link;
    }
    *pplhs = *pprhs = nullptr;
}