33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream.h>
struct node
{
node *next;
int d;
};
typedef struct node Sqlist;
class chainlist
{
private:
node *head;
node *now;
public:
chainlist()
{
head=NULL;
now=NULL;
};
void init();
void Delete(int locate,int number);
void out();
void add(node *a);
};
void chainlist::init()
{
now=head;
};
void chainlist::add(node *a)
{
if(head==NULL)
head=a;
else
{
now->next=a;
a->next=NULL;
now=now->next;
}
};
void chainlist::out()
{
node *b;
b=head;
while(b!=NULL)
{
cout<<b->d<<endl;
b=b->next;
}
};
void chainlist::Delete(int locate,int number)
{
node *h,*temp;
temp=head;
for(int i=0;i!=locate;++i)
temp=temp->next;
for(int ii=0;ii!=number;++ii)
{
h=temp;
temp=temp->next;
h->next=temp->next;
temp=NULL;
}
};
#include<iostream.h>
#include"chainlist.h"
void main()
{
Sqlist *a=new Sqlist[10];
chainlist ch;
ch.init();
for(int num=0;num!=10;++num) //这里往下可能有问题
{
a->d=2;
a->next=NULL;
ch.add(a);
a=a->next;
}
ch.out();
delete [] a;
}
#define MAX_LINK_NUM 20000
typedef struct Node
{
string name;
struct Node *next;
}node;
//初始化一个链表,link_num为初始节点个数,返回头节点
node *init_link(int link_num)
{
node *head = NULL,*cur = NULL,*pre = NULL;
for(int i=0; i<link_num; i++)
{
cout<<"input the "<<i+1<<"th data:";
cur = new node;
cur->next = NULL;
if(i == 0)
{
head = cur;
}
if(pre != NULL)
{
pre->next = cur;
}
pre = cur;
cin>>cur->name;
}
return head;
}
//打印链表
void print_link(const node *head)
{
while(head)
{
cout<<head->name<<endl;
head = head->next;
}
}
//删除链表
void delete_link(node *head)
{
node *next = NULL;
while(head)
{
next = head->next;
delete head;
head = next;
}
}
//按key值删除某个指定的节点,并返回头节点给形参head
bool delete_node(node *&head,string key)
{
node *pre = NULL,*cur = NULL;
if(key.length() == 0)
{
return false;
}
if(head->name == key) //删除的是头节点,特殊处理
{
cur = head->next;
delete head;
head = cur;
return true;
}
pre = head;
cur = head->next;
while(cur)
{
if(cur->name == key) //查找命中
{
pre->next = cur->next;
delete cur;
return true;
}
pre = cur;
cur = cur->next;
}
return false;
}
//插入节点
bool insert_node(node *head,string data)
{
node *new_node = NULL;
while(head->next)
{
head = head->next;
}
new_node = new node;
if(new_node)
{
new_node->name = data;
new_node->next = NULL;
head->next = new_node;
return true;
}
return false;
}
int main()
{
int link_num = 0;
bool bRet = true;
string str;
node *head = NULL,*tail = NULL;
cout<<"Input link_num:";
cin>>link_num;
head = init_link(link_num);
print_link(head);
delete_link(head);
return 0;
}
for(int num=0;num!=10;++num)
{
Sqlist *a=new Sqlist; //new出来的,用完需要delete
a->d=num;
a->next=NULL;
ch.add(a);
}
ch.out();
ch.destroy(); //添加一个函数用来delete链表中的Node指针
for(int num=0;num!=10;++num)
{
Sqlist *a=new Sqlist;
a->d=num;
a->next=NULL;
ch.add(a);
delete [] a;
}
ch.out();
Sqlist *a=new Sqlist;
for(int num=0;num!=10;++num)
{
a->d=num;
a->next=NULL;
ch.add(a);
}
ch.out();
delete [] a;
void chainlist::add(node *a)
{
if(head==NULL)
{
head=a;
now=head;
}
else
{
now->next=a;
// a->next=NULL;
now=now->next;
}
};
for(int num=0;num!=10;++num)
{
a->d=num;
a->next=NULL;
ch.add(a);
delete [] a;
}
for(int num=0;num!=10;++num) //这里往下可能有问题
{
a->d=2;
a->next=NULL; //a->next是NULL
ch.add(a);
a=a->next; //a是NULL,当第二次循环时,a->d=2; 会出错。
}
for(int num=0;num!=10;++num) //这里往下可能有问题
{
a->d=2;
a->next=NULL; //a->next是NULL
ch.add(a);
a=a->next; //a是NULL,当第二次循环时,a->d=2; 会出错。
}