用c++写的链表
2016-05-31 18:18
302 查看
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; struct node { int data; node *next; }; class listt { private: node *Head; public: void creat(); void display(); void Insert(int i,int x); int Delete(int i); }; void listt::Insert(int i,int x) { node *p,*q,*s; int k=1; q=Head; p=Head->next; while(k<i&&p!=NULL) { q=p; p=p->next; k++; } if(k==i) { s=new node; s->data=x; q->next=s; s->next=p; cout<<"插入成功"<<endl; } else cout<<" 插入失败"<<endl; } void listt::creat() { node *last,*p; int num; Head=last=NULL; scanf("%d",&num); while(num>0) { p=(node *) malloc(sizeof(node)); p->data=num; p->next=NULL; if(Head==NULL) Head=p; else last->next=p; last=p; scanf("%d",&num); } } void listt::display() { node *p; if(Head!=NULL) { p=Head; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } else cout<<"空表"<<endl; return ; } int listt::Delete(int i) { node *p,*q,*s; int x,k=1; q=Head; p=Head->next; while(k<i&&p!=NULL) { q=p; p=p->next; k++; } if(p!=NULL) { x=p->data; q->next=p->next; printf("删除成功\n"); } else { printf("删除失败\n"); x=-1; } return x; } int main() { listt A; A.creat(); int m,n,v; printf("输入你想插入的位置和插入的数,用空格隔开\n"); cin>>m>>n; A.Insert(m,n); printf("输入你想删除的位置\n"); cin>>v; A.Delete(v); A.display(); return 0; }
相关文章推荐
- g++和gcc 的makefile 写法
- fifo的c语言实现
- 第10周 C语言程序设计(新2版) 3.5例题 函数atoi(s),将字符串转换为对应数值
- c++实现快速排序
- c++ primer(第五版)学习笔记及习题答案代码版(第十三章)拷贝控制
- 第10周 C语言程序设计(新2版) 练习3-2 复制字符串,不可显示符号转为可示
- Eclipse C++语法高亮设置/背景色设置
- C语言几个经典的算法
- c++实现归并排序
- 高速C/C++编译工具ccache
- 深入解读C++中的右值引用
- C语言变长数组 struct中char data[0]的用法
- rabbitmq c++ rpc
- c++实现希尔排序
- c++实现插入排序
- C语言练习篇-2交换两个变量的值
- 第10周 C语言程序设计(新2版) 第三章例题 swich统计数字、字符和其它符号
- c++实现选择排序
- C++之中this指针与类的六个默认函数小结
- Unable to load DLL 'xxx.dll': 找不到指定的模块。 (Exception from HRESULT: 0x8007007E)