C/C++逆序打印链表

C 逆序打印链表
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXSIZE 128
#define SIZE 10

typedef struct ListNode* List;

struct ListNode{
	int val;
	struct ListNode* next;
};

typedef struct{
	List nodes[SIZE];
	int top;
}Stack;

int InitStack(Stack **stack) {
	*stack = (Stack*)malloc(sizeof(Stack));
	if(*stack == NULL)
		return -1;
	(*stack)->top = -1;
}

int Push(Stack *stack, List node) {
	if (stack->top == SIZE - 1)
		return -1;
	stack->top++;
	stack->nodes[stack->top] = node;
	printf("%d\t", node->val);
	return 0;
}

int Pop(Stack *stack, List *node) {
	if (stack->top == -1) 
		return -1;
	*node = stack->nodes[stack->top];
	stack->top--;
}

void PushList(Stack *stack, List head) {
	List t = head->next;
	while(t) {
		Push(stack, t);
		t = t->next; 
	}
	printf("\n");
}

void PopList(Stack *stack) {
	List t;
	while(stack->top != -1) {
		Pop(stack, &t);
		printf("%d\t", t->val);
	}
	printf("\n");
}

List CreateList1(List head, int *data, int size) {
	//头插法
	List p;
	int i;
	head->next = NULL;
	
	for(i = 0; i < size; i++){
		p = (List)malloc(sizeof(struct ListNode));
		p->val = data[i];
		p->next = head->next;
		head->next = p;
	}
	
	return head;
} 

List CreateList2(List head, int *data, int size) {
	//尾插法 
	int i;
	List p, q;
	
	p = q = head;
	
	for(i = 0; i < size; i++) {
		p = (List)malloc(sizeof(struct ListNode));
		p->val = data[i];
		q->next = p;
		q = p;
	}
	q->next = NULL;
	
	return head;
}

List ReverseList(List head) {
	List last = NULL, next = NULL, p;
	
	p = head->next;
	
	while(p) {
		next = p->next;
		p->next = last;
		last = p;
		p = next;
	}
	head->next = last;
	return head;
}

void PrintList(List head) {
	List t = head->next;
	while(t) {
		printf("%d\t", t->val);
		t = t->next; 
	}
	printf("\n");
}

List Recursion(List head) {
	if (head == NULL)
		return NULL;
	if (head->next) {
		Recursion(head->next);
	}
	printf("%d\t", head->val);
} 


int main(void)
{
	List list;
	int data[SIZE], i;
	Stack *stack;
	
	for(i = 0; i < SIZE; i++) {
		data[i] = i;
	}
	
	list = (List)malloc(sizeof(struct ListNode));
	list->next = NULL;
	
	list = CreateList2(list, data, SIZE);
	PrintList(list);
	
	//通过反转链表实现链表逆序打印 
//	printf("list reverse...\n");
//	list = ReverseList(list);
//	PrintList(list);

    //通过递归逆序打印链表 
//	printf("call recursion print list...\n");
//	Recursion(list->next);
	
	//通过栈逆序打印链表 
	InitStack(&stack);
	printf("push list into stack...\n");
	PushList(stack, list);
	printf("pop list outto stack...\n");
	PopList(stack);
	
	return 0;
}

c++ 逆序打印链表

#include <stdio.h>
#include <string.h>
#include <stack>   
#include <iostream>
#include <stdlib.h>

typedef struct ListNode* List;

struct ListNode{
	int val;
	struct ListNode* next;
};

List CreateList(List head, int* data, int size){
	int i;
	List p, q;
	p = q = head;
	
	for(i = 0; i < size; i++) {
		p = (List)malloc(sizeof(struct ListNode));
		p->val = data[i];
		q->next = p;
		q = p;
	}
	q->next = NULL;
	
	return head;
}

void PrintList(List head) {
	List p = head->next;
	
	while(p){
		printf("%d\t", p->val);
		p = p->next;
	}
	printf("\n");
}

void PrintReverse(List head){
	std::stack<List> list;
	List p;
	
	if(head == NULL)
		return;
	
	p = head->next;
	
	while(p){
		list.push(p);
		p = p->next;
	}
	
	while(!list.empty()){
		p = list.top();
		std::cout << p->val << "\t";
		list.pop();
	}
	std::cout<<std::endl;
}

int main(void)
{
	List head;
	int i, len = 0, data[20];
	head = (List)malloc(sizeof(struct ListNode));
	
	for(i = 0; i < 10; i++) {
		data[i] = i;
	}

	head = CreateList(head, data, 10);
	PrintList(head);
	
	std::cout << "reverse print list..." << std::endl;
	PrintReverse(head);
	return 0;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页