单链表逆序
2014-07-14 13:39
169 查看
非常简单,就当练个手吧
//单链表逆序问题,其实很容易的,就是把指针指向给变一下,注意的几个问题 //(1)如果就一个元素,不算头结点,直接返回 //(2)注意头结点最后要单独处理问题 #include <iostream> using namespace std; typedef struct linknode{ int val; linknode * next; }node,*list; //加typedef说明node和list是类型,否则只是一个struct的变量 void reverse(list head) { if(head==NULL||head->next==NULL||head->next->next==NULL) return; node * p=head->next; node * q=p->next; node * t; p->next=NULL; while(q!=NULL) { t=q->next; q->next=p; p=q; q=t; } head->next=p; } list createlist() { int data; list head=new node; head->val=0; head->next=NULL; node * p=head; while(cin>>data) { node * tmp=new node; tmp->val=data; tmp->next=NULL; p->next=tmp; p=tmp; } return head; } int main() { node * head=createlist(); reverse(head); node *p=head->next; while(p) { node *q=p->next; cout<<p->val<<" "; delete p; p=q; } cout<<endl; system("pause"); }
相关文章推荐
- [记着玩]java模拟链表逆序输出结果
- 数据结构实验之链表二:逆序建立链表
- 链表逆序输出(递归妙)
- 数据结构之 线性表 逆序简历链表
- 程序员面试宝典之数据结构基础---⑤单链表逆序的递归与非递归实现
- 单向链表之逆序(C语言实现)
- 数据结构实验之链表二:逆序建立链表
- 数据结构实验之链表二:逆序建立链表
- 逆序链表
- 链表元素的逆序输出
- C++实现链表逆序打印、链表反转
- 单链表中的一些经典问题--约瑟夫环,逆序,查找,复杂链表复制,链表带环问题
- 【C语言】链表逆序
- 链表逆序
- SDUT 2117 数据结构实验之链表二:逆序建立链表
- 单链表逆序的C++实现
- 单链表逆序
- 单链表逆序
- 单链表的逆序
- 算法学习十四----逆序输出链表