双向链表 删除某个节点
2016-05-05 09:36
288 查看
#include<iostream> using namespace std; typedef struct Node { int value; struct Node *prev; struct Node *next; }*Linklist, Node; Linklist CreateList(Linklist &L,int n) { int i; Linklist s,h; L = (Linklist)malloc(sizeof(Node)); h = L; scanf("%d",&L->value); L->prev = NULL; L->next = NULL; for(i=1;i<n;i++) { s = (Linklist)malloc(sizeof(Node)); scanf("%d",&s->value); L->next = s; s->prev = L; L = s; } // printf("aaaa %d\n",h->next->value); s->next = NULL; return h; } int cal_len(Linklist head) { int count=1; Linklist temp1 = head; while(temp1->next != NULL) { temp1 = temp1->next; count++; } return count; } Linklist remove_list_node(Linklist &head,int index) { Linklist temp; Linklist temp1; Linklist head_temp; int i; temp = head; head_temp = head; if(index == 0) { head->next->prev=NULL; head = head->next; temp->next = NULL; return head; // printf("temp->value=%d,head->value=%d\n",temp->value,head->value); // printf("%d\n",temp->value); } else if(index == cal_len(head)-1) { for(i=0;i<index-1;i++) { head = head->next; } temp1 = head->next; head->next->prev = NULL; head->next = NULL; return head_temp; // printf("%d\n",temp1->value); } else { for(i=0;i<index-1;i++) { head = head->next; } temp1 = head->next; head->next->next->prev = head; head->next = head->next->next; return head_temp; // printf("%d\n",temp1->value); } } int main() { Linklist L,head,head_temp1; int n,index; int i; scanf("%d",&n); //个数 scanf("%d",&index); //要删除的数的下表(从0开始) // int head_temp1; head = CreateList(L,n); // printf("%d\n",cal_len(head)); head_temp1 = remove_list_node(head,index); for(i=0;i<n-1;i++) { printf("%d\n",head_temp1->value); head_temp1 = head_temp1->next; } return 0; }
相关文章推荐
- Svchost.exe占用内存过大解决
- 21天学通Python笔记(二)
- JavaScript权威指南 第七章 数组
- 《C++精英内参之程序员高效指南》-2如何管理你的文档系统
- mysql之视图详解
- python编码问题 极大似然 2016.05.04回顾
- Apache 访问控制
- c++实验五—项目1:数组分离
- 红黑树删除操作
- local认证
- jquery.ajax之beforeSend用法
- Eratosthenes 法找素数 (含代码)
- Windows Server 2012 托管服务账号
- Read and write excel file with format by pandas and xlwt libraries of python
- 视图用法详解
- 《Motion Design for iOS》(十一)
- 用 rewrite 禁止目录
- ASP.NET-Active Direcotry编程示例
- 将文件转为byte[],通过ByteArrayOutputStream 实现
- CSS3 选择器 文字阴影