1. 双向链表 2. 顺序栈 3. 顺序队列

1. 双向链表 

功能函数

#include <stdio.h>
#include <stdlib.h>
#include "./_data.h"


/*
 * function:    创建双向链表头结点
 * @param [ in] 
 * @param [out] 
 * @return      
 */
two_linklist* creat_two_linklist(void)
{
	two_linklist* head = (two_linklist*)malloc(sizeof(two_linklist));
	if(NULL == head)
	{
		printf("头结点申请失败\n");
		return NULL;
	}
	head->last=NULL;
	head->next=NULL;
	head->gcf.len=0;//头节点初始化
	return head;
}


/*
 * function:    头插
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void insert_top(two_linklist* head,user_type value)
{
	//创建节点
	two_linklist* temp = (two_linklist*)malloc(sizeof(two_linklist));
	if(NULL == temp)
	{
		printf("结点申请失败\n");
		return;
	}
	temp->gcf.data=value;
	//处理next
	temp->next=head->next;
	head->next=temp;
	//处理last
	temp->last=head;
	if(temp->next!=NULL)//判断后面有无节点,是否队后面节点的last做操作
	{
		temp->next->last=temp;
	}

	head->gcf.data++;
}


/*
 * function:    尾插
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void insert_bottom(two_linklist* head,user_type value)
{
	//创建节点
	two_linklist* temp = (two_linklist*)malloc(sizeof(two_linklist));
	if(NULL == temp)
	{
		printf("结点申请失败\n");
		return;
	}
	two_linklist *p=head;//复制头结点地址
	temp->gcf.data=value;
	for(;p->next!=NULL;p=p->next);//找到尾节点
	//处理next
	temp->next=p->next;
	p->next=temp;
	//处理last
	temp->last=p;

	head->gcf.len++;
	
}



/*
 * function:    头删
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void delete_top(two_linklist* head)
{
	if(NULL == head->next)
	{
		printf("链表空空如也\n");
		return;
	}
	//另存要删除的节点地址
	two_linklist* temp=head->next;
	//处理next
	head->next=head->next->next;
	//处理last
	if(head->next!=NULL)
	{
		head->next->last=head;
	}
	free(temp);
	head->gcf.len--;
}



/*
 * function:    尾删
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void delete_bottom(two_linklist* head)
{
	if(NULL == head->next)
	{
		printf("链表空空如也\n");
		return;
	}

	two_linklist* p=head;

	for(;p->next!=NULL;p=p->next);//找最后一个节点

	//另存要删除的节点地址
	two_linklist* temp=p;
	//处理next
	p->last->next=p->next;
	//尾部last不需要处理
	
	free(temp);
	head->gcf.len--;
	return;
}


/*
 * function:    位置插入
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void insert_index(two_linklist* head,user_type value,int index)
{
	two_linklist * p = head;
	//要求一下,不用len做索引判断
	int len = 0;
	for(;p->next!=NULL;p=p->next,len++)
	{
		if(index == len+1)
			break;
	}//p指向的是插入位置的前一个节点
	if(index<=0 || index>len+1)
	{
		printf("索引位置不合法\n");
		return;
	}
	//创建节点
	two_linklist* temp = (two_linklist*)malloc(sizeof(two_linklist));
	if(NULL == temp)
	{
		printf("结点申请失败\n");
		return;
	}
	temp->gcf.data=value;
	//处理next,p已经在插入位置的前一个节点了
	temp->next=p->next;
	p->next=temp;
	//处理last
	temp->last=p;
	if(NULL!=temp->next)
	{
		temp->next->last=temp;
	}

	head->gcf.len++;
	
}


/*
 * function:    位置删除
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void delete_index(two_linklist* head,int index)
{
	if(index<=0 || index>head->gcf.len)
	{
		printf("索引位置不合法\n");
		return;
	}
	two_linklist * p = head;
	for(;index>1;index--,p=p->next);
	//创建节点
	two_linklist* temp = (two_linklist*)malloc(sizeof(two_linklist));
	if(NULL == temp)
	{
		printf("结点申请失败\n");
		return;
	}
	//另存要删除的节点地址
	two_linklist* d=p->next;
	//处理next
	p->next=p->next->next;
	//处理last
	if(p->next!=NULL)
	{
		p->next->last=p;
	}

	free(d);
	head->gcf.len--;
}







/*
 * function:    打印链表
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void put_two_linklist(two_linklist*head)
{
	if(NULL == head->next)
	{
		printf("链表空空如也\n");
		return;
	}
	two_linklist* p=head;
	printf("next打印:");
	while(p->next!=NULL)
	{
		printf("%-4d",p->next->gcf.data);
		p=p->next;
	}
	printf("\tlen=%d\n",head->gcf.len);

	printf("last打印:");
	while(p->last!=NULL)
	{
		printf("%-4d",p->gcf.data);
		p=p->last;
	}
	printf("\tlen=%d\n",head->gcf.len);
}

头文件

#ifndef __DATA_H__
#define __DATA_H__


typedef int user_type;

typedef struct node
{
	union
	{
		int len;
		user_type data;
	}gcf; 			//数据域
	struct node* next;
	struct node* last;//指针域
}two_linklist;



two_linklist* creat_two_linklist(void);//创建双向链表头结点

void insert_top(two_linklist* head,user_type value);//头插

void put_two_linklist(two_linklist*head);//打印链表

void insert_bottom(two_linklist*,user_type value);//尾插

void delete_top(two_linklist* head);//头删

void delete_bottom(two_linklist* head);//尾删

void insert_index(two_linklist* head,user_type value,int index);//位置插入

void delete_index(two_linklist* head,int index);//位置删除









#endif

主函数

#include <stdio.h>
#include "./_data.h"
int main(int argc, const char *argv[])
{

	two_linklist* head=creat_two_linklist();

	insert_top(head,1);
	insert_top(head,2);
	insert_top(head,3);
	put_two_linklist(head);
printf("\n-----------我是分界线------------\n");
	insert_bottom(head,4);
	insert_bottom(head,5);
	insert_bottom(head,6);
	put_two_linklist(head);

	delete_top(head);
	delete_top(head);
	delete_top(head);
	put_two_linklist(head);

printf("\n-----------我是分界线------------\n");
	delete_bottom(head);
	delete_bottom(head);
	delete_bottom(head);
	put_two_linklist(head);

	delete_bottom(head);
	delete_top(head);
	put_two_linklist(head);
printf("\n-----------我是分界线------------\n");

	insert_index(head,10,0);
	insert_index(head,30,1);
	insert_index(head,40,2);
	insert_index(head,20,1);
	insert_index(head,50,4);
	insert_index(head,10,6);
	put_two_linklist(head);

printf("\n-----------我是分界线------------\n");

	delete_index(head,0);
	delete_index(head,5);
	delete_index(head,1);
	delete_index(head,2);
	delete_index(head,2);
	put_two_linklist(head);


	return 0;
}

运行结果

2. 顺序栈列  

功能函数

#include <stdio.h>
#include <stdlib.h>
#include "./_data.h"

/*
 * function:    创建空栈
 * @param [ in] 
 * @param [out] 
 * @return      
 */
stack* creat_stack(void)
{
	stack* head = (stack*)malloc(sizeof(stack));
	if(NULL==head)
	{
		printf("创建空栈失败\n");
		return NULL;
	}
	head->pos=0;
}

/*
 * function:    入栈
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void insert_stack(stack* head,user_type value)
{
	if(head->pos>=N)
	{
		printf("栈已满,无法入栈\n");
			return;
	}
	head->data[head->pos]=value;
	head->pos++;
}


/*
 * function:    出栈
 * @param [ in] 
 * @param [out] 
 * @return      
 */
user_type delete_stack(stack* head)
{
	if(head->pos==0)
	{
		printf("栈没有数据,无法出栈\n");
		return(user_type)-1;
	}
	head->pos--;
	return head->data[head->pos];
}




/*
 * function:    打印栈
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void put_stack(stack* head)
{
	if(0==head->pos)
	{
		printf("栈空空如也\n");
		return;
	}
	for(int i=0;i<head->pos;i++)
	{
		printf("%-4d",head->data[i]);
	}
	printf("\n");
	return;
}






/*
 * function:    释放栈
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void free_stack(stack** head)
{
	free(*head);
	*head=NULL;
}

头文件

#ifndef __DATA_H__
#define __DATA_H__

#define N 3

typedef int user_type;
typedef struct
{
	user_type data[N];
	int pos;
}stack;


stack* creat_stack(void);//创建空栈

void insert_stack(stack* head,user_type value);//入栈

user_type delete_stack(stack* head);//出栈













void put_stack(stack* head);//打印栈

void free_stack(stack** head);//释放栈











#endif

主函数

#include <stdio.h>
#include "./_data.h"
int main(int argc, const char *argv[])
{

	stack* head = creat_stack();
	insert_stack(head,1);
	insert_stack(head,2);
	insert_stack(head,3);
	insert_stack(head,4);
	put_stack(head);

	printf("出栈:%d\n",delete_stack(head));
	printf("出栈:%d\n",delete_stack(head));
	printf("出栈:%d\n",delete_stack(head));
	printf("出栈:%d\n",delete_stack(head));
	put_stack(head);
	free_stack(&head);
	return 0;
}

运行结果

3. 顺序队 

功能函数

#include <stdio.h>
#include <stdlib.h>
#include "./_data.h"


int empty(sequeue* sq)//判断是否为空
{
	return (sq->head==sq->tail)?1:0;//返回1为空
}
int full(sequeue* sq)//判断是否为满
{
	return ((sq->tail+1)%(N+1)==sq->head)?1:0;//返回1为满
}

/*
 * function:    创建队列
 * @param [ in] 
 * @param [out] 
 * @return      
 */
sequeue* creat_sequeue(void)
{
	sequeue* sq = (sequeue*)malloc(sizeof(sequeue));
	if(NULL==sq)
	{
		printf("申请失败\n");
		return NULL;
	}
	sq->head=0;
	sq->tail=0;
	return sq;
}


/*
 * function:    入队
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void insert_sequeue(sequeue* sq,user_type value)
{
	if(full(sq))
	{
		printf("队列已满\n");
		return;
	}
	sq->data[sq->tail]=value;
	sq->tail=(sq->tail+1)%(N+1);
	return;
}




/*
 * function:    出队
 * @param [ in] 
 * @param [out] 
 * @return      
 */

user_type leavteam_sequeue(sequeue* sq)
{
	if(empty(sq))
	{
		printf("队列已空\n");
		return (user_type)-1;
	}
	user_type temp = sq->data[sq->head];
	sq->head = (sq->head+1)%(N+1);
	return temp;
}






/*
 * function:    打印
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void put_sequeue(sequeue* sq)
{
	if(empty(sq))
	{
		printf("队列已空\n");
		return;
	}
	int i = sq->head;
	while(i!=sq->tail)
	{
		printf("%-4d",sq->data[i]);
		i=(i+1)%(N+1);
	}
	printf("\n");
}





头文件

#ifndef __DATA_H__
#define __DATA_H__

#define N 3
typedef int user_type;
typedef struct 
{
	user_type data[N+1];
	int head;
	int tail;
}sequeue;



sequeue* creat_sequeue(void);//创建队列

void insert_sequeue(sequeue* sq,user_type value);//入队

user_type leavteam_sequeue(sequeue* sq);//出队

void put_sequeue(sequeue* sq);//打印








#endif

主函数

#include <stdio.h>
#include "./_data.h"
int main(int argc, const char *argv[])
{

	sequeue* sq=creat_sequeue();

	insert_sequeue(sq,1);
	insert_sequeue(sq,2);
	insert_sequeue(sq,3);
	insert_sequeue(sq,4);
	put_sequeue(sq);
	
	printf("%d\n",leavteam_sequeue(sq));
	printf("%d\n",leavteam_sequeue(sq));
	printf("%d\n",leavteam_sequeue(sq));
	printf("%d\n",leavteam_sequeue(sq));

	insert_sequeue(sq,1);
	insert_sequeue(sq,2);
	insert_sequeue(sq,3);
	insert_sequeue(sq,4);
	put_sequeue(sq);
	return 0;
}

运行结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值