链表的逆置

单链表的逆置

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

typedef char ElemType;

typedef struct Node{
	ElemType data;
	struct Node *next;
}Node, *LinkList;

LinkList CreateList()
{
	LinkList L;
	ElemType c;
	L = (LinkList)malloc(sizeof(Node));
	L->next = NULL;
	Node *p , *tail;
	tail = L;
	c = getchar();
	while(c != '#')
	{
		p = (Node *)malloc(sizeof(Node));
		p->data = c;
		tail->next = p;
		tail = p;
		c = getchar();
	}
	tail->next = NULL;
	return L;
}

void ShowList(LinkList L)
{
	Node *p;
	p = L->next;
	while(p != NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}

void ReverseList(LinkList L)
{
	Node *p, *q;
	p = L->next;
	L->next = NULL;
	while(p != NULL)
	{
		q = p->next;
		p->next = L->next;
		L->next = p;
		p = q;
	}

}

int main()
{
	LinkList L;
	L = CreateList();
	ShowList(L);

	ReverseList(L);
	ShowList(L);
	return 0;
}


posted @ 2013-03-22 16:28  N3verL4nd  阅读(189)  评论(0编辑  收藏  举报