单链表及系列操作
来源:互联网 发布:淘宝网已买到的宝贝 编辑:程序博客网 时间:2024/05/16 10:51
单链表
#include <iostream>#include <cstdlib>using namespace std;typedef struct Node{ int data; struct Node *next;}Node, *LinkList;void input1(LinkList *head){ LinkList p,r; int x; cin>>x; *head=(LinkList)malloc(sizeof(Node)); r=*head; while(x!=-1) { p=(Node *)malloc(sizeof(Node)); p->data=x; r->next=p; r=p; cin>>x; } r->next=NULL;}void input2(LinkList *head){ LinkList p; *head=(LinkList)malloc(sizeof(Node)); (*head)->next=NULL; int x; cin>>x; while(x!=-1) { p=(LinkList)malloc(sizeof(Node)); p->data=x; p->next=(*head)->next; (*head)->next=p; cin>>x; }}void print(LinkList head){ LinkList p; p=head; int cnt=1; while(p->next!=NULL) { cout << p->next->data << ' '; if(cnt%5==0) cout << endl; p=p->next; cnt++; } cout << endl;}void printsum(LinkList head){ LinkList p; p=head; int sum=0; while(p->next!=NULL) { sum+=p->next->data; p=p->next; } cout << sum << endl;}int find(LinkList head,int pos){ LinkList p; p=head->next; int j=1; while(p!=NULL&&j<pos) { p=p->next; j++; } return p->data;}void inser(LinkList *head,int pos,int e){ LinkList p,s; int j=1; p=*head; while(p&&j<pos) { p=p->next; j++; } s=(LinkList)malloc(sizeof(Node)); s->data=e; s->next=p->next; p->next=s;}void dele(LinkList *head,int pos){ LinkList p,q; int j=1; p=*head; while(p->next!=NULL&&j<pos) { p=p->next; j++; } q=p->next; p->next=q->next; free(q);}void clearr(LinkList *head){ LinkList p,q; p=(*head)->next; while(p) { q=p->next; free(p); p=q; } (*head)->next=NULL;}int main(){ LinkList head=NULL; input1(&head); print(head); printsum(head); cout << find(head,2) << endl; inser(&head,1,10); print(head); dele(&head,1); print(head); return 0;}
#include <iostream>#include <cstdlib>using namespace std;typedef struct Node{ int data; struct Node *next;}Node;Node *initLinkList(){ Node *head; head=(Node *)malloc(sizeof(Node)); head->next=NULL; return head;}void CreatLinkList(Node *head){ Node *p,*r; int x; cin>>x; r=head; while(x!=-1) { p=(Node *)malloc(sizeof(Node)); p->data=x; p->next=NULL; r->next=p; r=p; cin>>x; }}void printLinkList(Node *head){ Node *p; p=head->next; while(p!=NULL) { cout << p->data << ' '; p=p->next; } cout << endl;}void insertLinkList(Node *head,int x){ Node *pre,*r,*q; q=(Node *)malloc(sizeof(Node)); q->data=x; q->next=NULL; pre=head; r=head->next; while(r->data<x&&r!=NULL) { pre=r; r=r->next; } pre->next=q; q->next=r;}void deleteLinkList(Node *head){ Node *p,*q; p=head->next; while(p!=NULL) { q=p; p=p->next; free(q); } head->next=NULL;}Node *findLinkList(Node *head,int x){ Node *p; p=head->next; while(p!=NULL) { if(p->data==x) return p; p=p->next; } return NULL;}int main(){ Node h; h.next=NULL; //Node *h;h=initLinkList(); CreatLinkList(&h); insertLinkList(&h,6); //deleteLinkList(&h); printLinkList(&h); return 0;}
#include<cstdio>
#include<cstdlib>#include<iostream>using namespace std;typedef struct Node{ int data; struct Node *next;}Node;void creatLinkList(Node *h){ Node *p,*r; int x; cin>>x; r=h; while(x!=-1) { p=(Node *)malloc(sizeof(Node)); p->data=x; p->next=NULL; r->next=p; r=r->next; cin>>x; }}void printLinkList(Node *h){ Node *p; p=h->next; while(p!=NULL) { cout << p->data << ' '; p=p->next; } cout << endl;}void deletesdata(Node *h,int x)//删除某一元素{ Node *p,*pre,*c; pre=h; p=h->next; while(p!=NULL) { if(p->data==x) { c=p; pre->next=p->next; p=p->next; free(c); } else { pre=p; p=p->next; } }}void deleteparddata(Node *h)//删除重复元素{ Node *p; p=h->next; while(p!=NULL) { deletesdata(p,p->data); p=p->next; }}void ten_to_eight(Node *h,int n)//十进制转八进制{ Node *p,*r; r=h; while(n!=0) { int t; t=n%8; p=(Node *)malloc(sizeof(Node)); p->data=t; p->next=NULL; p->next=r->next; r->next=p; n/=8; }}void mergedata(Node *h1,Node *h2,Node *h3)//链表的合并{ Node *a,*b,*c,*p,*r; a=h1->next; b=h2->next; r=h3; while(a!=NULL) { r->next=a; r=r->next; a=a->next; } while(b!=NULL) { int flag=0; p=h3->next; while(p!=NULL) { if(b->data==p->data) { flag=1; break; } p=p->next; } if(flag==0) { b->next=NULL; r->next=b; r=r->next; } b=b->next; }}int main(){ Node h; h.next=NULL; //int n; //creatLinkList(&h); //deleteparddata(&h); //printLinkList(&h); //cin>>n; //ten_to_eight(&h,n); /*Node h1,h2,h3; h1.next=NULL; h2.next=NULL; h3.next=NULL; creatLinkList(&h1); creatLinkList(&h2); mergedata(&h1,&h2,&h3);*/ creatLinkList(&h); printLinkList(&h); return 0;}
阅读全文
0 0
- 单链表及系列操作
- 顺序表及系列操作
- 单链表操作系列
- PHP学习系列之文件访问及文件操作
- Git学习系列(二)创建本地仓库及文件操作
- Git学习系列(二)创建本地仓库及文件操作
- scala fold系列函数及 sparkRDD fold 操作解析
- MongoDB学习系列(一)mongo简介及相关操作
- 从无到有系列之Hive-入门操作及简单应用02
- ZooKeeper系列3:ZooKeeper命令、命令行工具及简单操作
- 单链表及相关操作
- Linux命令-基本操作及文件操作常用命令--详解Linux入门到精通系列
- Linux 命令图解5分钟系列[2] - 文件操作及目录操作进阶
- C语言单链表常见操作系列
- 单链表的创建及操作
- 单链表的创建及操作
- [推荐][提供下载](Excel):常用函数公式及操作技巧系列文章【共十篇】
- Android下SQLite数据库编程学习系列之一---熟悉SQLite及命令行操作
- 在ArcGIS中如何快速查找所要查看的要素?
- 学习网络通信框架
- 我所理解的大学-好友问答
- 操作系统
- Linux用户和文件权限管理
- 单链表及系列操作
- 模板方法模式
- Java String内存模型
- grep 命令系列:grep 中的正则表达式
- 2018笔试题——求一元一次方程的解
- 剑指offer 2 替换空字符串
- 解决jparepository.saveandflush插入不保存的情况
- Dell precision 5520工作站Ubuntu系统安装
- Git学习笔记--基础知识