单向链表增删
#include <myhead.h>
typedef struct a
{
int date;
struct a *next;
}aa;
//创建节点
aa *Creat()
{
aa *s=(aa*)malloc(sizeof(aa));
s->date=0;
s->next=NULL;
return s;
}
//头插
aa *insert(aa *head,int num)
{
aa *s=Creat();
s->date=num;
if(NULL==head)
{
head=s;
return head;
}
s->next=head;
head=s;
return head;
}
//显示
void show(aa *head)
{
if(head==NULL)
return;
aa*p=head;
while(p!=NULL)
{
printf("%d\t",p->date);
p=p->next;
}
puts("");
}
//头删
aa *del(aa *head)
{
if(NULL==head)
return head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
aa*p=head;
head=head->next;
free(p);
return head;
}
int main(int argc, const char *argv[])
{
aa *head=NULL;
int n;
printf("please enter n:");
scanf("%d",&n);
int num;
for(int i=0;i<n;i++)
{
printf("please enter num:");
scanf("%d",&num);
head=insert(head,num);
}
show(head);
head=del(head);
show(head);
return 0;
}
双向链表的增删:
#include <myhead.h>
typedef struct a
{
int date;
struct a *next;
struct a *front;
}aa;
//创建节点
aa *Creat()
{
aa *s=(aa*)malloc(sizeof(aa));
s->date=0;
s->next=NULL;
s->front=NULL;
return s;
}
//头插
aa *insert(aa *head,int num)
{
aa *s=Creat();
s->date=num;
if(NULL==head)
{
head=s;
return head;
}
s->next=head;
head->front=s;
head=s;
return head;
}
//显示
void show(aa *head)
{
if(head==NULL)
return;
aa*p=head;
while(p!=NULL)
{
printf("%d\t",p->date);
p=p->next;
}
puts("");
}
//头删
aa *del(aa *head)
{
if(NULL==head)
return head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
aa*p=head;
head=head->next;
free(p);
return head;
}
int main(int argc, const char *argv[])
{
aa *head=NULL;
int n;
printf("please enter n:");
scanf("%d",&n);
int num;
for(int i=0;i<n;i++)
{
printf("please enter num:");
scanf("%d",&num);
head=insert(head,num);
}
show(head);
head=del(head);
show(head);
return 0;
}