一个创建单链表的程序

麻烦大家给看看 程序有错误,大家帮忙看看哪里错了,谢谢!!! LinkList CreateList(LinkList &head) //顺序建表 { head=new Node; head->next=NULL; Node *q=head; int flag=1; while(flag) { int x; cin>>x; if(x!=0) { Node *p=new Node; p->data=x; p->next=NULL; q->next=p; q=p; } else flag=0; } return head; } void Print(LinkList &head) { Node *p; p=head->next; while(p) { cout< data<<" "; p=p->next; } } int main() { LinkList L; CreateList(L); Print(L); return 0; }
#include<iostream> using namespace std; enum yaya { error,success }; struct node { int data; node*next; }; class List{ public: List(); ~List(); int length(); yaya set_data(int n); node* get_ptr(int m); yaya inset_node(int x,int y); int delete_node(int i); void put_in(int x); void breaktwo(List&la,List&lb); void same_data(List&l1,List&l2,List&l); int out_data(); private: int count; node* head; }; List::List(){ head=new node; head->next=NULL; count=0; } int List::length(){ return count; } yaya List::set_data(int n){ node*p=head; for(int j=0;j<n;j++){ cin>>p->data; p=p->next; count++; if(p==NULL){ return error; } } return success; } node* List::get_ptr(int m){ node*p=head; int j=0; while(p!=NULL&&j<m){ j++; p=p->next; } return p; } yaya List::inset_node(int x,int y){ if(x<=0||x>count+1){ return error; } node*p=new node; p->data=y; node*current=head; for(int j=1;j<x;j++){ current=current->next; } p->next=current->next; current->next=p; count++; return success; } int List::delete_node(int i){ if(i<=0||i>count) return NULL; node*current=head; for(int j=1;j<i;j++){ current=current->next; }node*u=current->next; current->next=u->next; delete u; count--; return success; } void List::put_in(int x){ node*p=new node; p->data=x; node*current=head; for(;current!=NULL&&current->next!=NULL&&current->next->data<x;){ current=current->next; } p->next=current->next; current->next=p; count++; return; } void List::breaktwo(List& la,List& lb){ node*q=head->next; la.inset_node(la.count+1,q->data); while(q!=NULL&&q->next!=NULL&&q->next->next!=NULL){ q=q->next->next; la.inset_node(la.count+1,q->data); } node*w=head->next->next; lb.inset_node(lb.count+1,w->data); while(w!=NULL&&w->next!=NULL&&w->next->next!=NULL){ w=w->next->next; lb.inset_node(lb.count+1,w->data); } } void List::same_data(List&l1,List&l2,List&l){ node*p=l1.head; for(int i=1;i<=l1.count;i++){ p=p->next; node*q=l2.head; for(int j=1;j<=l2.count;j++){ q=q->next; if(p->data==q->data) l.inset_node(l.length()+1,p->data); } } } int List::out_data(){ node*p=head; for(int j=1;j<=count;j++){ p=p->next; cout<<(p->data)<<" "; } cout<<endl; return 0; } List::~List(){ node*p=head,*s; while(p!=NULL){ s=p; p=p->next; delete s; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值