链表操作
来源:互联网 发布:ibm云计算平台 编辑:程序博客网 时间:2024/06/05 17:46
#include<stdio.h>#include<iostream>using namespace std;struct stack{ // 定义栈结构,用于实现链表反向输出 int data[100]; int top;};struct node{ int data; struct node *next;};struct node *createList(int n){ //创建n个结点链表 struct node *head; head=(struct node *)malloc(sizeof(struct node)); struct node *p=head; struct node *q; for(int i=1;i<n;i++){ q=(struct node *)malloc(sizeof(struct node)); cin>>p->data; p->next=q; p=q; } cin>>p->data; p->next=NULL; return head;}void printlistnode(struct node* head){ // 正想打印链表 struct node* p=head; while(p != NULL){ cout<<p->data<<" "; p=p->next; } cout<<endl;}void reverseprintlistnode(struct node *head){ //反向打印链表 利用栈结构实现 struct node* p=head; struct stack s; s.top=0; while(p != NULL){ s.data[s.top] = p->data; ++s.top; p=p->next; } while(s.top>0){ --s.top; cout<<s.data[s.top]<<" "; } cout<<endl;}//递归实现反向打印链表结点void reverseprintlistnode_acle(struct node *head){ if(head != NULL){ if (head->next != NULL) reverseprintlistnode_acle(head->next); cout<<head->data<<" "; }}int main(){ int n; struct node* head; cin>>n; head=createList(n); printlistnode(head); reverseprintlistnode(head); reverseprintlistnode_acle(head); cout<<endl; free(head); return 0;}
0 0
- 实现链表基本操作(简单操作)
- 链表操作源程序
- 链表操作
- C++ 链表操作
- 链表的操作
- 链表操作源程序
- 链表的操作
- 链表的操作
- C++链表操作
- 链表操作
- 链表操作
- 链表基本操作
- LinkList链表操作
- 合并链表操作
- 链表简单操作
- 链表的操作
- 链表操作
- 操作链表
- .text()
- hadoop
- linux 安装tomcat
- Android UI开发第二十四篇——Action Bar
- 深度学习(八)caffe源码学习-未完待续
- 链表操作
- ThinkPHP 6.使用上传模块(ajaxFileUpload)
- 【学习OpenCV】第一章 安装OpenCV
- Cocos2D粒子发射器的纹理
- Makefile中的$(@:_config=)什么意思?
- shiro安全框架扩展教程--如何扩展实现集中式session管理(redis,memcached等)
- jqXHR对象
- php的RSA加密解密算法
- “钱三篇”后续之中国利率