在c中反转链表

时间:2016-08-02 10:32:09

标签: c

void reverse(node *pointer) {
    node *head, *curr;
    head = pointer;
    curr = head;
    while (head != NULL) {
        while (curr -> next != NULL) {
            curr = curr -> next;
        }
        printf("%d", curr -> data);
        free(curr);
        curr = head;
    }
}

在尝试撤消链接列表的最后一个节点时,我不知道代码的错误是什么。

3 个答案:

答案 0 :(得分:1)

简单地:

node *reverse(node *head){
    node *result = NULL;
    while (head) {
        node *next = head->next;
        head->next = result;
        result = head;
        head = next;
    }
    return result;
}

答案 1 :(得分:0)

您将无法看到上述函数的输出有两个原因2.按值传递而不是引用void reverse(struct node** head_ref){ struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev; } 函数内部没有打印语句。一旦功能出现,该功能的效果就会被破坏。

$check = new testClass;

// API services to loop through
$services = array(
    "dns" => "domains/search.json",
    "webservices" => "webservices/search.json",
    "singledomainhostinglinuxus" => "singledomainhosting/linux/us/search.json",
    "singledomainhostinglinuxuk" => "singledomainhosting/linux/uk/search.json"
);

// foreach service, assign a key to identify the data in the display
foreach ($services as $key => $value) {
    $data[$key] = $check->getData($value);
}

// Let's see what we got
echo "<pre>" . print_r($data, TRUE) . "</pre>";

class testClass {

    function getData($api) {
        $fullurl = "https://myapipath/" . $api . "?" . $this->buildstring();
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, $fullurl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch);
            curl_close($ch);
        return json_decode($data, true);
    }

    // Array key => value pairs
    private $parts = array();

    public function add($key, $value) {
        $this->parts[] = array(
            'key' => $key,
            'value' => $value
        );
    }

    // Build the query string
    public function buildstring($separator = '&', $equals = '=') {
        $queryString = array();

        foreach ($this->parts as $part) {
            $queryString[] = urlencode($part['key']) . $equals . urlencode($part['value']);
        }

        return implode($separator, $queryString);
    }

    // recursive function
    public function request($service, $page) {
        $count = 10; // 10 records is the minimum allowed to request
        $this->add(array('no-of-records', $count));
        $this->add(array('page-no', $page));
        $data = $this->getData(array($service, TRUE));

        if ($data[0]['recsindb'] > $page * $count) {
            $data = $this->request($service, $page + 1);
        }
        return $data;
    }
}

答案 2 :(得分:-1)

也许你可以试试这个

void reverse(node * pointer){

node *head, *curr;
head = pointer;
curr = head;
while(head != NULL){
    while(curr -> next != NULL){
        curr = curr -> next;
        printf("%d",curr -> data);
        curr = head;
    }
}
free(curr);

}