删除链表中一个位于中间位置的节点
来源:互联网 发布:java函数返回值为数组 编辑:程序博客网 时间:2024/05/17 03:09
Problem
Delete a node in the middle of list, given only access list once
Solution
#include <iostream>using namespace std;typedef struct linked_list{ int data; struct linked_list *next;}Linked_list;int add_node(Linked_list **head, int d){ Linked_list *l = new Linked_list; if(l == NULL) return 0; Linked_list *t = *head; l->data = d; l->next = NULL; if(*head == NULL){ *head = l; return 1; } while(t->next != NULL){ t = t->next; } t->next = l; return 1;}void del_mid_elem(Linked_list *head, Linked_list *l){ Linked_list *t; if(head->next == NULL && head->next == NULL){ return; } t = l->next; l->data = l->next->data; l->next = l->next->next; delete t;}void delete_list(Linked_list *head){ while(head != NULL){ Linked_list *t = head->next; delete head; head = t; }}int main(int argc, char* argv[]){ Linked_list *head = NULL; Linked_list *l; add_node(&head, 1); add_node(&head, 1); add_node(&head, 2); add_node(&head, 3); add_node(&head, 9); add_node(&head, 4); add_node(&head, 1); add_node(&head, 5); cout << "before" << endl; l = head; while(l!=NULL){ cout << l->data << endl; l = l->next; } l = head; while(l!=NULL){ if(l->data == 9) break;; l = l->next; } cout << "after deleting " << l->data << endl; del_mid_elem(head, l); l = head; while(l!=NULL){ cout << l->data << endl; l = l->next; } delete_list(head); return 0;}
Output
before11239415last 0 in the listlast 1 in the list5last 2 in the list15
- 删除链表中一个位于中间位置的节点
- 获取当前节点位于父节点的位置
- 一个单链表,不知道长度,写一个函数快速找到中间节点的位置
- 一个单链表,不知道长度,写一个函数快速找到中间节点的位置
- 删除链表的中间节点
- 删除链表的中间节点
- 删除链表的中间节点
- CareerCup2.3 删除最中间那个,或者删除给出任意一个节点(只能得到这个节点的pointer)
- 007 算法:从单链表中删除一个中间节点
- 要寻找一个单链表的中间节点
- 求一个单链表的中间节点
- 快速定位到单链表的中间位置的节点
- 只遍历一次单链表,确定单链表中间节点的位置
- 使li横排显示并且ul位于div的中间位置
- 单链表 算法(删除中间节点)
- 删除链表中间节点
- 删除链表中间节点
- 关于长单链表中间节点删除
- c++ 程序员 你 要具备什么?
- 多线程 NSthread
- Oracle 学习笔记5 —— profile文件的维护
- android窃取用户信息新思路
- Python multi-process VS multi-thread (多核CPU利用率)
- 删除链表中一个位于中间位置的节点
- 关于CentOS系统自带BCM 5709网卡驱动版本Bug问题
- SQL Server 2005中触发器监控数据库变化
- ORA-20000: ORU-10027: 执行存储过程的错误
- 关于memcache分布式一致性hash
- Android4.1 compile problem
- ldconfig命令作用
- 自定义toast
- 中序遍历和先序遍历/后序遍历构建二叉树