算法研究之——链表的一些操作(创建、打印,在头部、中间、尾部插入节点)
2014-11-11 23:35
531 查看
#include<iostream> using namespace std; struct Node { int data; Node* next; }; typedef class Node List_Node; //1.链表初始化 void init_list(Node *head) { head = nullptr; cout << "the list have been inited" << endl; } /*********************************************************************/ //2.创建单链表:该方法生成的节点顺序与输入的顺序一致而且表头不为空即包含数据 /***********************************************************************/ List_Node *creat_list() { List_Node *head = nullptr; List_Node *p1, *p2; p1 = p2 = new Node;//p1为工作指针,p2为移动指针 cout << "plz input data:"; cin >> p1->data;//创建一个新的节点,从cin读入数据 p1->next = nullptr;//新的节点的next指向null while (p1->data > 0)//这里把输入的数据 { if (head == nullptr) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = new Node; cin >> p1->data; p1->next = nullptr; } return head; } /******************************************/ //3.第二种方法建立链表,节点和输入的顺序是相反的 /*******************************************/ List_Node* creat_list2() { Node *head=nullptr; Node*p1; p1 = new Node; cin >> p1->data; while (p1->data > 0) { p1->next = head; head = p1; p1 = new Node; cin >> p1->data; } return head; } /*************************************************/ //4.输出链表:即大家所说的打印链表print the list /*************************************************/ void print_list(Node *Head) { if (Head == nullptr) { cout << "empty list" ; } else { cout << "the list is:"; while (NULL != Head) { cout << Head->data<<" "; Head = Head->next; } } cout << endl; } /**************************************************/ //5.清除链表:clear the list /**************************************************/ void clear_list(Node* head) { Node *pNext; if (head = nullptr) { cout << "the list is empty" << endl; } else { while (nullptr != head) { pNext = head->next; delete head; head = pNext;//表头下移动 } cout << "the list had been emptied" << endl; } } /**************************************************/ //6.return the list zize /**************************************************/ int size_list(Node* head) { int size = 0; while (head != nullptr) { size++; head = head->next;//the head move forward next step } return size; } /**************************************************/ //7.return the n element /**************************************************/ int get_element(Node*head,int n) { int i = 0; if (n < 0) { cout << "n is not a available value" << endl; } if (head == nullptr) { cout << "the list is empty" << endl; } while (head != NULL) { ++i; if (i == n) { break; } head = head->next; } if (i < n) { cout << "the list's len is shorter then n" << endl; } return head->data; } /*****************************************************************************************/ //8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL /*****************************************************************************************/ int *get_element_addr(Node*head, int x) { if (nullptr == head) { cout << "the list is empty" << endl; } while ((head->data != x) && (head->next != nullptr)) { head = head->next; } if (head->data == x) { cout << x << " " << &(head->data) << endl; } return &(head->data); } /*****************************************************************************************/ //9. 把链表中第n个节点的值修改为x,成功返回1,失败返回0 /*****************************************************************************************/ int modify_list(Node *head, int n, int x) { int i = 0; if (nullptr == head) { cout << "the list is empty" << endl; } while (head != nullptr) { ++i; if (i == n) { break; } head = head->next; } if (i < n) { cout << "n out of the range" << endl; } head->data = x; return 1; } /*****************************************************************************************/ //10. 在表的头部插入一个新的节点成功返回true insert a new node into the head of the list /*****************************************************************************************/ bool insert_head_Node(Node **head, int insertelem)//这里把整个链表作为输入才能修改整个链表,不然就 { //想当与传值参数而无法修改链表 Node *insert_node=new Node; insert_node->data = insertelem; insert_node->next = *head; *head = insert_node; return 1; } /*****************************************************************************************/ //11. insert the new node at the end of the list /*****************************************************************************************/ bool insert_end_Node(Node **head, int insertelem) { Node *pHead = *head; Node *pTemp = new Node;//临时的节点用来存储数据 Node *insert_node = new Node; insert_node->data = insertelem; insert_node->next = nullptr; pTemp = pHead; while (pHead->next != nullptr) { pHead = pHead->next; } pHead->next = insert_node; *head = pTemp; return true; } /*****************************************************************************************/ //11. /*****************************************************************************************/ int main() { Node *my_list; my_list = creat_list2();//use the second way to creat forward list. print_list(my_list);//print the list. int len = size_list(my_list);//output the length of the list. cout << len << endl; int value = get_element(my_list,3);//输出第三个节点的数据 cout << value << endl; cout << "修改后的"; modify_list(my_list, 3, 20); print_list(my_list); cout << "在头部插入新的节点后"; insert_head_Node(&my_list, 30); print_list(my_list); cout << "在尾部插入元素后"; insert_end_Node(&my_list, 100); print_list(my_list); } //还有待解决的问题 /* 向有序单链表中插入元素x结点,使得插入后仍然有序 */ /* 从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行 */ /* 从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行 */ /* 从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行 */ /* 从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0 */ /* 交换2个元素的位置 */ /* 将线性表进行快速排序 */
相关文章推荐
- 链表的基本操作 从头部插入 尾部插入 中间插入 删除节点
- C-链表的一些基本操作【创建-删除-打印-插入】
- 算法学习-数据结构之链表操作,创建,插入,删除,查找。
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- java语言编写链表的基本操作(链表的创建,插入,删除,打印,排序)
- 单链表的创建,表长,插入,查找,逆置,中间元素,删除节点,打印
- 常见算法:C语言中链表的操作(创建,插入,删除,输出)
- 链表操作--创建--删除--插入--打印
- 链表操作综合练习(创建、拆分、插入、删除、合并、打印、逆置)
- c语言链表基本操作(带有创建链表 删除 打印 插入)
- 树的基本结构,以及利用链表实现树的各项操作(创建、添加/删除/打印树节点、销毁等等)
- 线性表的链式存储格式基本操作:创建链表、插入、删除、查找、求表长、打印链表
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- 用头插、尾插、按顺序插入创建一个不带头节点的链表,栈的基本操作
- 链表面试题逆序打印,删除无头非尾节点,非头位置插入,约瑟夫环,查找中间和倒数第k个节点
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- 一.从零写单链表的算法之尾部&头部插入节点
- 双向链表容器list的头部、尾部、中间的插入
- 单链表的建立(头部延长、尾部延长)、插入操作、删除操作(无头结点的删除、有头结点的删除)
- 单链表反转,单链表创建,单链表尾部插入,单链表打印