【文件属性】:
文件名称:我编写的实现单链表的程序
文件大小:567KB
文件格式:RAR
更新时间:2012-07-12 14:18:06
单链表
#include
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>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&&jnext;
}
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;jnext;
}
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;jnext;
}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&¤t->next!=NULL&¤t->next->datanext;
}
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<next;
delete s;
}
}
【文件预览】:
单链表
----单链表.sln(893B)
----单链表.ncb(1.55MB)
----Debug()
--------单链表.exe(53KB)
--------单链表.pdb(651KB)
--------单链表.ilk(717KB)
----单链表.suo(14KB)
----单链表()
--------task6.txt(121B)
--------task5.txt(67B)
--------单链表.vcproj.LS-PC.Administrator.user(1KB)
--------yay.h(2KB)
--------task1.txt(22B)
--------Debug()
--------task4.txt(30B)
--------单链表.vcproj(4KB)
--------yay.cpp(6KB)