链表的基本使用

链表的基本使用

分类:C++

链表的基本使用

创建链表,添加元素,删除元素

#include "iostream"using namespace std;struct node{int data;node *next;};class list{node *head;public:list(){head=NULL;}void insertlist(int a,int b);void deletelist(int a);void outputlist();node* gethead(){return head;}};void list::outputlist(){node *p=head;while (p!=NULL){cout<<p->data<<" ";p=p->next;}}void list::insertlist(int a,int b){node *q,*p,*s;s=new(node);s->data=b;p=head;if (head==NULL){head=s;s->next=NULL;}else if (p->data==a){s->next=p;head=s;}else{while (p->data!=a && p->next!=NULL){q=p;p=p->next;}if (p->data==a){q->next=s;s->next=p;}else{p->next=s;s->next=NULL;}}}void list::deletelist(int a){node *p,*q;p=head;if (p==NULL)return ;if (p->data==a){head=head->next;delete p;}else{while (p->data!=a && p->next!=NULL){q=p;p=p->next;}if (p->data==a){q->next=p->next;delete p;}}}int main(){list A,B;int x,n;cout<<"输入将要插入链表A的数字个数:";cin>>n;cout<<"输入"<<n<<"个数字:";while (n–){cin>>x;A.insertlist(0,x);}cout<<"链表A:";A.outputlist();cout<<"\n输入要删除的数字:";cin>>x;A.deletelist(x);cout<<"链表A:";A.outputlist();return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇HDU 5335 贪心+BFS下一篇二叉树的基本使用

顶0踩0

,你可以用爱得到全世界,你也可以用恨失去全世界

链表的基本使用

相关文章:

你感兴趣的文章:

标签云: