用C语言表达下

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:34:01
⑴ 用头插法(或尾插法)建立带头结点的单链表;
⑵ 对已建立的单链表实现插入、删除等基本操作。
还要注释,谢谢!!!
急!!!!快!!!

#include<iostream>
using namespace std;
int len=0;
struct list//结构的声明
{
int data;
list *next;
};
list *head;
list *create()//建立链表,这是第一步;
{
list *p,*q;
head=NULL;
int temp;
cout<<"Now create the list,Input the data,end by -1:"<<endl;
cin>>temp;
len=0;
while(temp!=-1)
{
len++;
p=new list;
p->data=temp;
if(head==NULL)
head=p;
else
{
q->next=p;
}
q=p;
cin>>temp;
}
if(head!=NULL)
q->next=NULL;
return head;
}
void display(list *head)//显示链表的所有数据。
{
if(head==NULL)
{
cout<<"The list is empty!"<<endl;
return;
}
cout<<"the list is:"<<endl;
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;