链表的基本操作(C++)
#include "stdafx.h" #include #include using namespace std; template struct Node { T data; Node }; template Node { //Node Node Node Node for (unsigned int i = 0; i < arr.size(); i++) { //p= (Node p = new Node pre->next = p; p->data = arr[i]; pre = p; } p->next = NULL; return head->next; } template void PrintList(Node { Node p->next = head; //为了方便遍历 while (p->next != NULL) { p = p->next; cout << p->data << "->"; } cout<<"NULL"<< endl; } template Node { Node while (cur != NULL) { temp = cur->next; cur->next = pre;//翻转 pre = cur; cur = temp; } return pre; } //插入第n个元素后面 template Node { Node int i = n - 1; while (i > 0) { p = p->next; i--; } Node q->data = val; q->next = p->next; p->next = q; return head; } //删除第n个元素后面 template Node { Node 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 arr = {1,2,3,4,5}; Node PrintList(head); /*Node PrintList(rhead);*/ Node PrintList(addhead); delNode(addhead,5); PrintList(addhead); getchar(); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |