1、单链表的创建
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*q,*p;
void build(int n)
{
while(n--)
{
p=(struct node*)malloc(sizeof(struct node));
if(!p)
{
printf("error");
exit(1);
}
scanf("%d",&p->data);
p->next=NULL;
if(head==NULL) head=p;
else q->next=p;
q=p;
}
return ;
}
int main()
{
int n;
cin>>n;
build(n);
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
2、查询
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*q,*p;
void build