单向链表
#include<myhead.h>
typedef struct Nodea
{
int data;
struct Nodea *next;
}*Node;
//创建链表
Node create_my()
{
Node s=(Node)malloc(sizeof(struct Nodea));
if(NULL==s)
return NULL;
s->data=0;
s->next=NULL;
return s;
}
//头插
Node insert_head(Node head,int num)
{
Node p = create_my();
p->data = num;
if(p == NULL)
return head;
if(head == NULL)
head = p;
else
{
p->next = head;
head = p;
}
return head;
}
//尾插
Node insert_tail(Node head,int num)
{
Node p = create_my();
p->data = num;
if(p == NULL)
return head;
if(head == NULL)
head = p;
else
{
Node s = head;
while(s->next==NULL)
{
s = s->next;
}
s->next = p;
}
return head;
}
//头删
Node del_head(Node head)
{
if(head == NULL)
return head;
else
{
Node temp = head;
head = head->next;
free(temp);
temp = NULL;
return head;
}
}
//尾删
Node del_tail(Node head)
{
if(head == NULL)
return head;
if(head->next==NULL)
{
free(head);head=NULL;
}
else
{
Node temp = head;
while(temp->next->next!=NULL)
{
temp = temp->next;
}
free(temp->next);
temp->next=NULL;
}
}
int main(int argc, const char *argv[])
{
//创建链表
Node head = NULL;
head = create_my(head);
//头插
int num = 1;
insert_head(head,num);
//尾插
num = 2;
insert_tail(head,num);
//头删
head = del_head(head);
//尾删
head = del_tail(head);
return 0;
}
双向链表
#include<myhead.h>
typedef struct Nodea
{
struct Nodea *front;
int data;
struct Nodea *next;
}*Node;
//创建链表
Node create_my()
{
Node s=(Node)malloc(sizeof(struct Nodea));
if(NULL==s)
return NULL;
s->data=0;
s->next=NULL;
s->front=NULL;
return s;
}
//头插
Node insert_head(Node head,int num)
{
Node p = create_my();
p->data = num;
if(p == NULL)
return head;
if(head == NULL)
head = p;
else
{
p->next = head;
head->front=p;
head = p;
}
return head;
}
//尾插
Node insert_tail(Node head,int num)
{
Node p = create_my();
p->data = num;
if(p == NULL)
return head;
if(head == NULL)
head = p;
else
{
Node s = head;
while(s->next==NULL)
{
s = s->next;
}
s->next = p;
p->front=s;
}
return head;
}
//头删
Node del_head(Node head)
{
if(head == NULL)
return head;
else
{
Node temp = head;
head = head->next;
head->front=NULL;
free(temp);
temp = NULL;
return head;
}
}
//尾删
Node del_tail(Node head)
{
if(head == NULL)
return head;
if(head->next==NULL)
{
free(head);head=NULL;
}
else
{
Node temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->front->next=NULL;
free(temp);
temp->next=NULL;
}
}
int main(int argc, const char *argv[])
{
//创建链表
Node head = NULL;
head = create_my(head);
//头插
int num = 1;
insert_head(head,num);
//尾插
num = 2;
insert_tail(head,num);
//头删
head = del_head(head);
//尾删
head = del_tail(head);
return 0;
}