求C++创建链表,及链表的基本操作(创建,插入)代码

来源:百度知道 编辑:UC知道 时间:2024/06/21 14:48:05
创建一个链表,不是线性表。最好是C++的。代码,不是算法

# include "iostream.h"
# include "stdlib.h"
# define NULL 0
typedef struct list{
int data;
struct list* next;
}list,*LIST;
void create(LIST& head){//创建链表
LIST p1,p2;
head=p1=p2=(LIST)malloc(sizeof(list));
cout<<"please input a int type num,quit by pressing 0\ndata: ";
cin>>p1->data;
for(;p1->data!=0;){
p2=p1;
p1=(LIST)malloc(sizeof(list));
cout<<"data: ";
cin>>p1->data;
p2->next=p1;
}
p2->next=NULL;
if(head->data==0)
head=NULL;
}
void insert(LIST& head){//把元素插入链表
LIST p;
p=(LIST)malloc(sizeof(list));
cout<<"please input a int type data you want to insert\ndata: ";
cin>>p->data;
p->next=head;
head=p;

}
void D