关于链表的递归操作
2015-11-07 12:22
344 查看
不带头结点链表翻转
思路: 假设有1,2,3,4,5,6,这样6个数构成一个链表,要进行翻转操作,结果应为6,5,4,3,2,1,
假设要翻转1 ,那就要先翻转2,,要翻转2,就要翻转3,直到要翻转6,就是当剩下一个元素或者链表为空时作为结束条件
假设2,3,4,5,6,已经翻转,此时结果为1,6,5,4,3,2,可见,我们只需将1移到2后面就行了,还可以看到1一直是指向2的,所以找到2的位置并不难。
思路: 假设有1,2,3,4,5,6,这样6个数构成一个链表,要进行翻转操作,结果应为6,5,4,3,2,1,
假设要翻转1 ,那就要先翻转2,,要翻转2,就要翻转3,直到要翻转6,就是当剩下一个元素或者链表为空时作为结束条件
假设2,3,4,5,6,已经翻转,此时结果为1,6,5,4,3,2,可见,我们只需将1移到2后面就行了,还可以看到1一直是指向2的,所以找到2的位置并不难。
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; typedef struct linklist { int data; struct linklist *next; }node; node *create() { node *head=NULL,*q,*p; int x; scanf("%d",&x); while(x!=0) { q=(node*)malloc(sizeof(node)); q->data=x; if(!head) head=p=q; else p->next=q,p=p->next; scanf("%d",&x); } p->next=NULL; return head; } node *create_head() { node *head,*q,*p; int x; head=(node*)malloc(sizeof(node)); p=head; scanf("%d",&x); while(x!=0) { q=(node*)malloc(sizeof(node)); q->data=x; p->next=q; p=p->next; scanf("%d",&x); } p->next=NULL; return head; } void display(node *head) { node *p=head; if(!head) { printf("error!\n"); return; } while(p) { printf("%-5d",p->data); p=p->next; } printf("\n"); } void display_head(node *head) { node *p=head->next; if(!p) { printf("error!\n"); return ; } while(p) { printf("%-5d",p->data); p=p->next; } printf("\n"); } void display2(node* head)//递归逆序输出链表的值 { if(head==NULL) return ; display2(head->next); printf("%-5d",head->data); } node *rever(node *head)//不带头结点的链表翻转 { if(head==NULL||head->next==NULL) return head;//空表或者只剩一个节点,直接返回 node *newhead = rever(head->next);//要翻转第一个,首先要翻转后面那些,把后面的看成一个整体 head->next->next=head;//假设后面的部分已经翻转完成,则要翻转点的后继要指向翻转点 head->next=NULL;//翻转后这个点指向为空 return newhead; } void rever_head(node *head,node *h)//带头结点的链表翻转 { if(h==NULL||h->next==NULL)//若链表为空或者只有一个元素 { head->next=h;//将头结点指向这个元素 return ; } rever_head(head,h->next); h->next->next=h; h->next=NULL; } node *delallx(node *head,int x) { node *p=head,*pre=NULL; if(!head) return head; while(p&&p->data!=x) pre=p,p=p->next;//找到第一个等于x的节点 if(p) { if(!pre) //若为头结点,继续找,返回第一个不为x点的地址 head=delallx(head->next,x); else pre->next=delallx(p->next,x); free(p); } return head; } void delallx_head(node *head,int x)//删除带头接节点链表的所有x { node *p=head->next,*pre=head; if(p==NULL) return; while(p&&p->data!=x) pre=p,p=p->next; if(p) pre=delallx(p->next,x),free(p); } int main() { node *head; // head=create_head(); // display_head(head); //rever_head(head,head->next); //display_head(head); //delallx(head,4); //display_head(head); head=create(); display2(head); printf("\n"); display(head); /*head=rever(head); display(head); head=delallx(head,3); display(head);*/ return 0; }
相关文章推荐
- (异常分析)hibernate异常:java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
- mtk lcd调试
- MTK Android Driver知识大全
- HDU 5396 Expression(区间DP,排列组合)
- Android控件动画
- at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)
- SQL Server判断对象是否存在 (if exists (select * from sysobjects )(转)
- POJ-2498
- SQL Server 死锁
- [leetcode] Expression Add Operators
- HDOJ 1081(ZOJ 1074) To The Max(动态规划)
- 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
- HDOJ 1081(ZOJ 1074) To The Max(动态规划)
- PowerDesigner打开pdm文件时报“打印错误”(解决)
- Android Security Model
- ifttt do note 新浪微博 地址
- Android SDK Manager
- 从来都没有忘记自己的梦想在何方
- [sicily]1935. 二叉树重建
- 使用html写一个android关于界面,事情原来可以如此简单