1. 程式人生 > >連結串列的基本操作(C++)

連結串列的基本操作(C++)

#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
struct Node {
	T data;
	Node<T> *next;
};
template <typename T>
Node<T> *CreatList(vector<T> &arr)
{
	//Node<T> *head = (Node<T>*)malloc(sizeof(Node<T>));
	Node<T> *head = new Node<T>;
	Node<T> *pre = head;
	Node<T> *p = NULL;
	for (unsigned int i = 0; i < arr.size(); i++)
	{
		//p= (Node<T>*)malloc(sizeof(Node<T>));
		p = new Node<T>;
		pre->next = p;
		p->data = arr[i];
		pre = p;

	}
	p->next = NULL;
	return head->next;
}
template <typename T>
void PrintList(Node<T> *head)
{
	Node<T> *p = new Node<T>;
	p->next = head; //為了方便遍歷
	while (p->next != NULL)
	{
		p = p->next;
		cout << p->data << "->";
	}
	cout<<"NULL"<< endl;
	
}
template <typename T>
Node<T>* reverseList(Node<T> *head)
{
	Node<T> *pre = NULL, *cur = head, *temp;
	while (cur != NULL)
	{
		temp = cur->next;
		cur->next = pre;//翻轉
		pre = cur;
		cur = temp;
		
	}
	
	return pre;
}
//插入第n個元素後面
template <typename T>
Node<T> * addNode(Node<T> *head, T val, int n)
{
	
	Node<T> *p = head;
	int i = n - 1;
	while (i > 0)
	{
		p = p->next;
		i--;
	}
	Node<T> *q = new Node<T>;//要插入的元素
	q->data = val;
	q->next = p->next;
	p->next = q;
	return head;
}
//刪除第n個元素後面
template <typename T>
Node<T> * delNode(Node<T> *head, int n)
{
	Node<T> *p = head;
	int i = n - 1;
	while (i > 0)
	{
		
		p = p->next;
		if (p->next == NULL)//最後一個元素,不作刪除
			return head;
		i--;
	}
	if (p->next->next == NULL)//倒數第二個(刪除最後一個)
		p->next = NULL;
	else
	 p->next = p->next->next;
	
	return head;
}
int main()
{
	vector<int> arr(5);
	arr = {1,2,3,4,5};
	Node<int> *head = CreatList(arr);
	PrintList(head);
	/*Node<int> *rhead = reverseList(head);
	PrintList(rhead);*/
	Node<int> *addhead = addNode(head, 8, 3);
	PrintList(addhead);
	delNode(addhead,5);
	PrintList(addhead);
	getchar();
	return 0;
}