单向,双向链表,并且实现两种链表的增加和删除功能

单向链表

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值