天天看点

c++链表的反转

#include "stdio.h"
#include "iostream"

using namespace std;

struct Node{
	int value;
	struct Node* next;
};

struct Node node;

struct Node* node_inverse(struct Node* node)
{
	struct Node* p, *q, *r, *ret;

	p = node;
	q = node->next;
	p->next = NULL;
	while (q){
		r = q->next;
		q->next = p;
		p = q;
		q = r;
	}

	ret = p;

	return ret;
	
}

void node_push(struct Node* node, int value)
{
	static int first_node = 0;

	if (node == NULL){
		return;
	}

	struct Node* temp = node;

	if (!first_node){
		node->value = value;
		node->next = NULL;
		first_node = 1;
	}
	else{
		while (temp->next != NULL){
			temp = temp->next;
		}

		struct Node* node_new = (struct Node*)malloc(sizeof(Node));

		node_new->next = NULL;
		node_new->value = value;

		temp->next = node_new;
	}

	
}

int main()
{
	struct Node node, *reverse;

	node_push(&node, 1);
	node_push(&node, 2);
	node_push(&node, 3);
	node_push(&node, 4);

	reverse = node_inverse(&node);

	return 0;
}

           

继续阅读