给一个不带头结点的单链表,写出将链表倒置的算法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_28633157/article/details/48982231
//头文件
#ifndef LIST_H
#define LIST_H

#include
#include
using namespace std;
template
class LinkNode{
public:
	T data;
	LinkNode* link;
	LinkNode()
	{

	}
	LinkNode(const T& item,LinkNode*ptr=NULL){
		data = item;
		link = ptr;
	}
};
template
class List{

private:
	LinkNode* head;
	LinkNode* currPos;
public:
	List();
	~List();
	void Append(const T value);//为链表赋值,规定链表长度
	void clear();
	void print();
	void HeadToTail();//将链表倒置
};
template
List::List()
{
	//默认构造函数
	head = NULL;
	currPos = NULL;
}

template
List::~List()
{
	clear();
}

template
void List::Append(const T value)
{
	if (head == NULL)
	{
		head = new LinkNode(value);
		currPos = head;
	}
	else
	{
		LinkNode* p = currPos;
		p->link = new LinkNode(value);
		p = p->link;
		p->link = NULL;
		currPos = p;
	}
}

template
void List::clear()
{
	LinkNode* p = head;
	while (p->link != NULL)
	{
		p = p->link;
		delete head;
		head = p;
	}
}

template
void List::print()
{
	LinkNode* p = head;
	while (p != NULL)
	{
		cout << p->data << " ";
		p = p->link;
	}
	cout << endl;
}

template
void List::HeadToTail()
{
	int top = -1;
	stacktemp;
	LinkNode* p = head;
	while (p != NULL)
	{
		temp.push(p->data);
		p = p->link;
	}
	p = head;
	while (p != NULL)
	{
		p->data = temp.top();
		p = p->link;
		temp.pop();
	}
}


#endif
//主函数
#include"List.h"

int main(int argc, char argv[])
{
	Listlist;
	int count;
	cout << "请输入单链表的长度:";
	cin >> count;
	int value;
	for (int i = 0; i < count; i++)
	{
		cin >> value;
		list.Append(value);
	}
	list.print();
	list.HeadToTail();
	list.print();
	return 0;
}
展开阅读全文

不带头结点单链表

04-30

[code=c]rn// list.hrn#ifndef SINGLE_LINKED_LIST_H_rn#define SINGLE_LINKED_LIST_H_rnrntypedef struct node rn int value;rn struct node *link;rn node;rnrnnode* creat();rnrnvoid destroy(node *header);rnrnvoid clear(node *header);rnrnvoid insert(node *header, int i, int value);rnrnvoid remove_by_index(node *header, int i);rnrnvoid remove_by_value(node *header, int value);rnrnnode* find_by_index(node *header, int i);rnrnint size(node* header);rnrnint is_empty(node *header);rnrnvoid display(node* header);rnrn#endif // SINGLE_LINEKD_LIST_H_rnrn// list.crn#include rn#include rn#include "list.h"rnrnnode* creat() rn node* head = NULL;rn node* tail = head;rn int n;rn while (scanf("%d", &n) == 1) rn if (head == NULL) rn head = malloc(sizeof(node));rn head->value = n;rn head->link = NULL;rn tail = head;rn rn else rn node* new = malloc(sizeof(node));rn new->value = n;rn new->link = NULL;rn tail->link = new;rn tail = new;rn rn rn return head;rnrnrnvoid destroy(node *head) rn clear(head);rnrnrnvoid clear(node *head) rn while (head != NULL) rn node* next = head->link;rn free(head);rn head = next;rn rn head = NULL;rnrnrnvoid insert(node *head, int i, int value) rn node* new = malloc(sizeof(node));rn new->value = value;rn if (i == 0) rn new->link = head;rn head = new;rn rn else rn node* prev = find_by_index(head, i - 1);rn new->link = prev->link;rn prev->link = new;rn rnrnrnvoid remove_by_index(node *head, int i) rn if (i == 0) rn node* curr = head;rn head = head->link;rn free(curr);rn rn else rn node* prev = find_by_index(head, i - 1);rn node* curr = prev->link;rn prev->link = curr->link;rn free(curr);rn rnrnrnvoid remove_by_value(node *head, int value) rn node* curr = NULL;rn if (head->value == value) rn curr = head;rn node* next = curr->link;rn free(curr);rn curr = next;rn rn elsern curr = head;rn while (curr != NULL) rn if (curr->value == value) rn node* next = curr->link;rn node* prev = head;rn while (prev->link != curr) rn prev = prev->link;rn prev->link = next;rn free(curr);rn curr = next;rn rn elsern curr = curr->link;rn rnrnrnnode* find_by_index(node *head, int i) rn node* curr = head;rn for (int j = 0; j < i; curr = curr->link, j++);rn return curr;rnrnrnint size(node* head) rn node* curr = head;rn int len = 0;rn while (curr != NULL) rn len++;rn curr = curr->link;rn rn return len;rnrnrnint is_empty(node *head) rn if (head == NULL)rn return 1;rn elsern return 0;rnrnrnvoid display(node* head) rn for (node* curr = head; curr != NULL; curr = curr->link)rn printf("%d ", curr->value);rnrnrn// main.crn#include rn#include rn#include "list.h"rnrnint main() rn node* head = creat();rn puts("Create");rn display(head); putchar('\n');rn/*rn puts("Insert");rn insert(head, 3, 100);rn display(head); putchar('\n');rnrn puts("Remove by index");rn remove_by_index(head, 3);rn display(head); putchar('\n');rnrn puts("Remove by value");rn remove_by_value(head, 4);rn display(head); putchar('\n');rn*/rn clear(head);rn puts("Size after clear");rn printf("%d\n", size(head));rn if (is_empty(head))rn printf("Is empty\n");rn elsern printf("Not empty\n");rnrn puts("Destroy");rn destroy(head); rn rn return EXIT_SUCCESS;rnrn[/code] 论坛

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