linuxc语言之单链表的使用包含stack和queue

#ifndef _MULTI_TIMER_H_
#define _MULTI_TIMER_H_
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>

typedef struct Timer_ {
    uint32_t        cur_ticks;          /* Record current timer start tick */
    uint32_t        cur_expired_time;   /* Record current timer expired time */
    uint32_t        timeout;    /* Delay (xx ms) time to start tiemr */
    uint32_t        repeat;     /* Timer interval expired time (xx ms) */
    void *          arg;        /* Input argument for timeout_cb function */
    void            (*timeout_cb)(void *arg); /* Timer expired callback function */
    struct Timer_*next;
} timer_node,*timerlinklist;


/*
It means 1 tick for 1ms. 
Your can configurate for your tick time such as 5ms/10ms and so on.
*/
#define CFG_TIMER_1_TICK_N_MS   1



timerlinklist list_init();
timer_node* node_create();
void list_free(timerlinklist L);
int list_empty(timerlinklist L);
int list_length(timerlinklist L);
void list_show(timerlinklist L);
timerlinklist list_create_head_insert(timerlinklist L);
timerlinklist list_create_tail_insert(timerlinklist L);
int list_insert(timerlinklist L,timer_node* data,int pos);
timer_node* list_get_elem(timerlinklist L,int pos);
int list_delete(timerlinklist L,int pos);
#endif




#include "timer_list.h"
#include <string.h>
#include <stdlib.h>



timerlinklist list_init()
{
	timerlinklist L = (timerlinklist)malloc(sizeof(timer_node));
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return NULL;
	}
	///printf("error create %s %d\n",__func__,__LINE__);
	L->next = NULL;
	return L;
}


timer_node* node_create()
{
	timer_node* pnode = (timer_node*)malloc(sizeof(timer_node));
	if(pnode==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return NULL;
	}
	//printf("error create %s %d\n",__func__,__LINE__);
	pnode->next = NULL;
	//printf("pnode->next %s %d\n",__func__,__LINE__);
	return pnode;
}


void list_show(timerlinklist L)
{
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return ;
	}
	
	timer_node* pnode = L->next;
	while(pnode)
	{
		printf("%s %d repeat is %d\n",__func__,__LINE__,pnode->repeat);
		pnode = pnode->next;
	}
}

int list_empty(timerlinklist L)
{
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}
	
	timer_node* pnode = L->next;
	
    if(pnode)
	{
		return 0;
	}
	else 
	{
		return 1;
	}
	
}

void list_free(timerlinklist L)
{
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return ;
	}
	timer_node*pnode,*ptmp;
	pnode = L->next;
	while(pnode)
	{
		//printf("%s %d \n",__func__,__LINE__);
		ptmp = pnode->next;
		free(pnode);
		memset(pnode,0,sizeof(timer_node));
		pnode = ptmp;
	}
	L->next = NULL;///
}

int list_length(timerlinklist L)
{
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}
	
	timer_node* pnode = L->next;
	int i = 0;
	while(pnode)
	{
	  //     printf("%s %d \n",__func__,__LINE__);
		pnode = pnode->next;
		i++;
	}
	//printf("%s %d \n",__func__,__LINE__);
	return i;
}


timerlinklist list_create_head_insert(timerlinklist L)
{
	int i  =0 ;
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return NULL;
	}
	timer_node* phead = L->next;
	timer_node* pnode = NULL;
	for(i=0;i<100;i++)   // 加入100 节   //头插法
	{
		pnode = node_create();
		pnode->repeat = i;
		pnode->next = phead;
		phead = pnode;
	}
	L->next = phead;
	return L;
}

timerlinklist list_create_tail_insert(timerlinklist L)  //
{
       int i  =0 ;
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return NULL;
	}
	timer_node* phead = L->next;
	timer_node* pnode = NULL;
	timer_node* prenode = NULL;
	for(i=0;i<100;i++)   // 加入100 节   //尾插法
	{
	
		pnode = node_create();
		pnode->repeat = i;
		//phead = pnode;
		//pnode
		while(phead)
		{
			prenode = phead;
			phead = phead->next;
		}
	//	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
		phead = pnode;
	
	   if(!L->next)
		{
			L->next = phead;
		//    printf("%s %d length is %d repeat is %d\n",__func__,__LINE__,list_length(L),phead->repeat);
		}
		   
		if(prenode)
		{
			 prenode->next= pnode;
		}
	   //    printf("%s %d length is %d repeat is %d\n",__func__,__LINE__,list_length(L),phead->repeat);
	}
	// printf("%s %d length is %d repeat is %pL->next  is%p\n",__func__,__LINE__,list_length(L),phead,L->next->next);
	return L;
}


int list_insert(timerlinklist L,timer_node* data,int pos)
{

	int i  =0 ;
	int length = -1;
	
	timer_node* phead = L->next;
	timer_node* prenode = NULL;
	
	if(L==NULL||data==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}

	if(pos<0)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}

	length = list_length(L);
	if(pos>=length)  //tail   insert
	{
		while(phead)
		{
			prenode = phead;
			phead = phead->next;
		}
		phead = data;
		
		if(!L->next)
		{
			L->next = phead;
		    // printf("%s %d length is %d repeat is %d\n",__func__,__LINE__,list_length(L),phead->repeat);
		}
		
		if(prenode)
		{
		  prenode->next= data;
		}
	}
	else if(pos==0)   //head  insert 
	{
		data->next = phead;
		phead = data;
		L->next = phead;
	}
	else 
	{
	    printf("%s %d length is %d repeat is %d\n",__func__,__LINE__,list_length(L),phead->repeat);
		while(phead&&i<pos)   //后插法 
		{
			prenode = phead;
			phead = phead->next;
			i++;
		}
		data->next = prenode->next;
		prenode->next = data;
	}

	return 0;
}

timer_node* list_get_elem(timerlinklist L,int pos)
{
	int i  =0 ;
	int length = -1;
	
	timer_node* phead = L->next;
	timer_node* prenode = NULL;
	
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return NULL;
	}

	if(pos<0)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return NULL;
	}

	length = list_length(L);
	while(phead&&i<pos)  
	{
		prenode = phead;
		phead = phead->next;
		i++;
	}
	
	return prenode;
}



int list_delete(timerlinklist L,int pos)
{
	int i  =0 ;
	int length = -1;
	
	timer_node* phead = L->next;
	timer_node* prenode = NULL;
	
	if(L==NULL)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}

	if(pos<=0)
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}

	length = list_length(L);

    printf("%d length %d",pos,length);
	if(pos>length)  //tail   insert
	{
		printf("error create %s %d\n",__func__,__LINE__);
		return -1;
	}
	else if(pos==length)
	{
	
		while(phead&&i<pos-1)   //找到前一个节点
		{
			prenode = phead;
			phead = phead->next;
			i++;
		}

        if(pos == 1)
		{
			printf("%s %d \n",__func__,__LINE__);
			free(phead);
			L->next = NULL;
			return 0;
		}

		printf("%s %d \n",__func__,__LINE__);
		free(prenode->next);
		prenode->next = NULL;		
		

	}
	else 
	{
		printf("%s %d \n",__func__,__LINE__);
		while(phead&&i<pos-1)   //找到前一个节点 
		{
			prenode = phead;
			phead = phead->next;
			i++;
		}
		
		if(pos == 1)    //删除第一个节点 要区分
		{
			printf("%s %d \n",__func__,__LINE__);
			//free(phead);
			phead = phead->next;
			free(L->next);
			L->next = phead;
			return 0;
		}
		
		phead = phead->next;
		printf("%s %d \n",__func__,__LINE__);
		//data->next = prenode->next;
		//prenode->next = data;
		//free(prenode);
		free(prenode->next);
		prenode->next = phead;
	}
	return 0;
}


#include "timer_list.h"
#include <stdio.h>
#include <signal.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>

static void test_list_node_create(void);
static void test_list_list_create(void);
static void test_list_list_create2();
static void test_list_basic_op(void);
static void test_list_stack(void);
static void test_list_queue(void);

int main()
{
	//test_list_node_create();
	//test_list_list_create();
	//test_list_list_create2();
	//test_list_basic_op();
	//test_list_stack();
	test_list_queue();
	while(1) 
	{
		if(getchar() == 'q')
		{
		  break;
		}
	}
	return 0;
}

static void test_list_node_create(void)
{
	int ret = -1;
	timer_node* ptimer_key;
	timer_node* ptimer_led;
	timerlinklist L = list_init();
	if(L==NULL)
	{
	   return ;
	}
	
	ret = list_empty(L);
	printf("%s %d ret is %d\n",__func__,__LINE__,ret);
	
	ptimer_key = node_create();
	if(ptimer_key==NULL)
	{
	   return ;
	}
	ptimer_key->repeat = 1000;
	ptimer_led = node_create();
	if(ptimer_led==NULL)
	{
	   return ;
	}
	ptimer_led->repeat = 2000;
	
	L->next = ptimer_key;
	ptimer_key->next  = ptimer_led;
	
	list_show(L);
	
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	ret = list_empty(L);
	
	list_free(L);
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	list_show(L);
}


static void test_list_list_create(void)
{
	timerlinklist L = list_init();
	if(L==NULL)
	{
	   return ;
	}
	
	L = list_create_head_insert(L);
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	list_show(L);
	list_free(L);
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	list_show(L);
}



static void test_list_list_create2(void)
{
	timerlinklist L = list_init();
	if(L==NULL)
	{
	   return ;
	}
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	L = list_create_tail_insert(L);   //节点放数据   指针右移 不能改变原来节点里面存放的数据
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	list_show(L);
	list_free(L);
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	list_show(L);
}


static void test_list_basic_op(void)
{
	timerlinklist L = list_init();
	timer_node* pnode = NULL;
	if(L==NULL)
	{
	   return ;
	}
	
	#if 0 
	pnode = node_create();
	pnode->repeat = 0;    // 尾部插入数据 
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,list_length(L));

	pnode = node_create();
	pnode->repeat = 2;
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 3;
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 4;
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 5;
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 6;
	list_insert(L,pnode,list_length(L));

	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));

	list_show(L);
	list_free(L);
	
	
	
	pnode = node_create();
	pnode->repeat = 0;    // 头部插入数据 
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,0);

	pnode = node_create();
	pnode->repeat = 2;
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 3;
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 4;
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 5;
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 6;
	list_insert(L,pnode,0);
	
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	
	list_show(L);
	list_free(L);
	
	
	
	
	pnode = node_create();
	pnode->repeat = 0;    // 头部插入数据 
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,0);

	pnode = node_create();
	pnode->repeat = 2;
	list_insert(L,pnode,0);
	

	
	
	pnode = node_create();
	pnode->repeat = 4;
	list_insert(L,pnode,2);
	
	pnode = node_create();
	pnode->repeat = 5;
	list_insert(L,pnode,3);
	
	//pnode = node_create();
//	pnode->repeat = 6;
//	list_insert(L,pnode,2);
	
	printf("%s %d length is %d\n",__func__,__LINE__,list_length(L));
	
	list_show(L);
	list_free(L);
	
	
	
	pnode = node_create();
	pnode->repeat = 0;    // 头部插入数据 
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,0);

	pnode = node_create();
	pnode->repeat = 2;
	list_insert(L,pnode,0);
	
	pnode = list_get_elem(L,2);
	printf("%s %d repeat is %d\n",__func__,__LINE__,pnode->repeat);
	list_show(L);
	list_free(L);
	#endif 
	
	                        //output  
							
	pnode = node_create();
	pnode->repeat = 0;    // 头部插入数据 
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,0);


	//pnode = list_get_elem(L,2);
	//printf("%s %d repeat is %d\n",__func__,__LINE__,pnode->repeat);
	//list_delete(L,2);
	//list_delete(L,2);
	list_delete(L,1);
	list_delete(L,1);
	list_show(L);
	list_free(L);
}

static void test_list_stack(void)
{
	timerlinklist L = list_init();
	timer_node* pnode = NULL;
	if(L==NULL)
	{
	   return ;
	}
	
	pnode = node_create();
	pnode->repeat = 0;    // 头部插入数据 
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,0);

	pnode = node_create();
	pnode->repeat = 2;
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 3;
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 4;
	list_insert(L,pnode,0);
	
	list_delete(L,1);
	list_delete(L,1);
	list_delete(L,1);
	list_delete(L,1);
	list_delete(L,1);
	
	list_show(L);
	
}

static void test_list_queue(void)
{
	timerlinklist L = list_init();
	timer_node* pnode = NULL;
	if(L==NULL)
	{
	   return ;
	}
	
	pnode = node_create();
	pnode->repeat = 0;    // 头部插入数据 
	list_insert(L,pnode,0);
	
	pnode = node_create();
	pnode->repeat = 1;
	list_insert(L,pnode,list_length(L));

	pnode = node_create();
	pnode->repeat = 2;
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 3;
	list_insert(L,pnode,list_length(L));
	
	pnode = node_create();
	pnode->repeat = 4;
	list_insert(L,pnode,list_length(L));
	
	list_delete(L,1);
	list_delete(L,1);
	list_delete(L,1);
	list_delete(L,1);
	list_delete(L,1);
	
	list_show(L);
	
}



在这里插入图片描述在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天6点半起床10点半睡觉和今天早晚运动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值