单链表/单循环链表/双链表/双循环链表_C语言实现

    本文对单链表/单循环链表/双链表/双循环链表_C语言进行实现。

    实现如下6个操作:

  1. 初始化init 
  2. 插入insert(指定插入位置和插入元素)
  3. 替换replace(用指定的元素替换某个位置的元素)
  4. del删除指定节点 
  5. find根据值查找某个节点,找到返回所在的pos,否则返回失败 
  6. prt打印整个链表

     首先对头结点、头指针的意义明确下

头结点(大话数据结构P58对头结点和头指针的比较分析):

头结点是为了操作的统一和方便设立的,放在第一元素的节点之前,其数据域一般无意义(也可存放链表长度)
头指针是指指向链表第一个节点的指针,如果没有头结点,头指针指向的就是第一个节点;如果有头结点,头指针指向的就是头结点。

    相关tips:

1.因为单链表必须要用头结点,否则在头位置的插入,删除都会很麻烦。--数据结构与算法分析C语言描述..P33页,

2.双链表也必须要用表头

3.对于init/insert/delete/prt/find 5个API,双链表在单链表的基础上只是需要修改insert(插入时要修改插入节点后一节点的prev指针)和delete(插入时要修改插入节点后一节点的prev指针)两个API

3.循环链表有没有表头都可以(数据结构与算法分析C语言描述..P38页),为了和单链表、双链表保持一致,使用带表头的版本

4.单链表和单循环链表的区别

  将单链表的最后一个元素的next指针指向头结点,就成了单循环链表。

  单循环链表可以表中任一位置出发即可访问全部节点。而单链表只能访问当前位置之后的节点。

  单链表的终止条件;p!=NULL或p->next!=NULL,而单循环链表的终止条件是:p!=head或者p->next!=head

5.单链表和双链表的区别

  双链表多了个指向前趋元素的prev指针,每个节点都比单链表多占用一个指针大小的空间

  单链表只能实现单向查找,而双链表可以实现双向查找,对于单链表而言,要查找上一节点必须要遍历链表,时间复杂度是O(n),而双链表就非常方便了。

6.insert操作有两种,一种是指定插入的pos和ele,要直接插入在指定位置,第二种是不指定插入位置,默认插入在最后。第二种比较简单,只实现第一种

7.注意insert操作和replace操作,insert可以插入到链表末尾,比如链表的size=5,可以指定插入位置=6(插入到末尾)。而replace只能替换链表里已有的元素,所以终止条件有区别。这点在代码里要注意

上传单链表/单循环链表/双链表/双循环链表C语言实现版本:

单链表:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>  //signal()
#include<string.h>
#include<sys/stat.h>
#include<time.h>
#include<stdarg.h>

#if 1
#define INFO_DEBUG    "%d lines in "__FILE__", complie time is "__TIME__" "__DATE__" \n",__LINE__
#define ADI_RUN_ERROR      1
#define ADI_NULL_POINTER  -1
#define ADI_OK             0
#define ADI_PRINT         printf("[%s][%d]:",__FUNCTION__,__LINE__);printf
#define ADI_ASSERT(c,prt,ret)       if(c) {printf(prt);return ret;}
#endif

typedef struct list{
    int elem;
    struct list *next;
}list;

#define PARAM_FAIL -1
#define P_NULL -2
#define SUCCESS 0

/*初始化:只创建头结点*/
list* initList()
{
    list *head = (list*)malloc(sizeof(list));
    if(NULL==head) {printf("NULL");return head;}
    head->next = NULL;
    return head;
}

/*pos从0开始,pos=0为第一个*/
int insertList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && NULL != head)//i比head快一个,i=1时p指向的是第0个元素
    {
        head = head->next;
        i++;
    }

    if(NULL != head)
    {
		list *node = (list*)malloc(sizeof(list));
		if(NULL==node) {printf("NULL");return P_NULL;}
		node->next = head->next;
		node->elem = elem;
		head->next = node;
		ADI_PRINT("insert elem = %d\n",elem);
    }
    else
    {
       ADI_PRINT("pos err\n");
       return PARAM_FAIL;
    }

    return SUCCESS;
}


/*pos从0开始,pos=0为第一个*/
int replaceList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && NULL != head)//i比head快一个,i=1时head指向的是第0个元素
    {
        head = head->next;
        i++;
    }

    if(NULL != head && NULL != head->next)
    {
		head->next->elem = elem;
		ADI_PRINT("change elem = %d,pos = %d\n",elem,pos);
    }
    else
    {
       ADI_PRINT("pos err\n");
       return PARAM_FAIL;
    }

    return SUCCESS;
}

int delList(list *list_insert,int elem)
{
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;
    while(NULL != head->next)
    {
		if(elem == head->next->elem)
		{
		    list *del = head->next;
        	head->next = head->next->next;
        	free(del);
        	ADI_PRINT("del ele = %d\n",elem);
        	return SUCCESS;
		}
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",elem);
    return PARAM_FAIL;
}

/*pos为在链表中的位置,ele为待查找的元素*/
int findList(list *listFind,int ele,int *pos)
{
    if(NULL == listFind)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    int i = 0;
    list *head = listFind;
    while(NULL != head->next)
    {
		if(ele == head->next->elem)
		{
        	*pos = i;
        	return SUCCESS;
		}
		i++;
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",ele);
    return PARAM_FAIL;
}

int printList(list *list_prt)
{
    if(NULL == list_prt)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }
    int i = 0;

    while(NULL != list_prt->next)
    { 
        list_prt = list_prt->next;
        ADI_PRINT("list[%d] = %d\n",i,list_prt->elem);
        i++;
    }


    return SUCCESS;
}

int main()
{
    int ret = 0,pos = 0;
    list *list_test = initList();
    if(NULL == list_test)
    {
        ADI_PRINT("err \n");
        return P_NULL;
    }

    ret = insertList(list_test,0,0);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,3,1);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,5,2);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,7,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,9,4);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    printList(list_test);

    ret = findList(list_test,9,&pos);
    if(SUCCESS != ret) {ADI_PRINT("find err\n");return PARAM_FAIL;}
    ADI_PRINT("ele=9 pos = %d\n",pos);
    
    ret = delList(list_test,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,7);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,9);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;} 
    printList(list_test);

    ret = replaceList(list_test,2,2);
    if(SUCCESS != ret) {ADI_PRINT("change err\n");return PARAM_FAIL;}  
    printList(list_test);    

    
    return SUCCESS;
}



单循环链表:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>  //signal()
#include<string.h>
#include<sys/stat.h>
#include<time.h>
#include<stdarg.h>

#if 1
#define INFO_DEBUG    "%d lines in "__FILE__", complie time is "__TIME__" "__DATE__" \n",__LINE__
#define ADI_RUN_ERROR      1
#define ADI_NULL_POINTER  -1
#define ADI_OK             0
#define ADI_PRINT         printf("[%s][%d]:",__FUNCTION__,__LINE__);printf
#define ADI_ASSERT(c,prt,ret)       if(c) {printf(prt);return ret;}
#endif

typedef struct list{
    int elem;
    struct list *next;
}list;

#define PARAM_FAIL -1
#define P_NULL -2
#define SUCCESS 0

/*初始化:只创建头结点*/
list* initList()
{
    list *head = (list*)malloc(sizeof(list));
    if(NULL==head) {printf("NULL");return head;}
    head->next = head;
    return head;
}

/*pos从0开始,pos=0为第一个*/
/*如果循环链表插入操作指定插入位置pos,会麻烦些,如下,如果不指定位置,默认插入到最后,会比较简单*/
int insertList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && list_insert != head->next)
    {
        head = head->next;
        i++;
        //i比head快一个,i=1时head指向的是第0个元素
    }

    if((list_insert==head->next)&&(i<pos))//已经到链表尾部,但是pos>i,说明指定的插入位置超过了链表的末尾位置
    {
		ADI_PRINT("pos = %d, list's last = %d,pos>i,insert err!\n",pos,i);
		return PARAM_FAIL;
    }
    else
    {
		list *node = (list*)malloc(sizeof(list));
		if(NULL==node) {printf("NULL");return P_NULL;}
		node->next = head->next;
		node->elem = elem;
		head->next = node;
		ADI_PRINT("insert elem = %d\n",elem);
    }

    return SUCCESS;
}


/*pos从0开始,pos=0为第一个*/
int replaceList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && list_insert != head->next)
    {
        head = head->next;
        i++;
        //i比head快一个,i=1时head指向的是第0个元素
    }

    if(list_insert==head->next)//已经到链表尾部,说明指定的修改位置超过了链表原有大小
    {
		ADI_PRINT("pos = %d, list's last = %d,change err!\n",pos,i);
		return PARAM_FAIL;
    }
    else
    {
		head->next->elem = elem;
		ADI_PRINT("change elem = %d,pos = %d\n",elem,pos);
    }

    return SUCCESS;
}

int delList(list *list_insert,int elem)
{
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;
    while(list_insert != head->next)
    {
		if(elem == head->next->elem)
		{
		    list *del = head->next;
        	head->next = head->next->next;
        	free(del);
        	ADI_PRINT("del ele = %d\n",elem);
        	return SUCCESS;
		}
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",elem);
    return PARAM_FAIL;
}

/*pos为在链表中的位置,ele为待查找的元素*/
int findList(list *listFind,int ele,int *pos)
{
    if(NULL == listFind)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    int i = 0;
    list *head = listFind;
    while(listFind != head->next)
    {
		if(ele == head->next->elem)
		{
        	*pos = i;
        	return SUCCESS;
		}
		i++;
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",ele);
    return PARAM_FAIL;
}

int printList(list *list_prt)
{
    if(NULL == list_prt)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }
    int i = 0;

    list *head = list_prt;
    while(head != list_prt->next)
    { 
        list_prt = list_prt->next;
        ADI_PRINT("list[%d] = %d\n",i,list_prt->elem);
        i++;
    }


    return SUCCESS;
}

int main()
{
    int ret = 0,pos = 0;
    list *list_test = initList();
    if(NULL == list_test)
    {
        ADI_PRINT("err \n");
        return P_NULL;
    }

    ret = insertList(list_test,0,0);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,3,1);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,5,2);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,7,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,9,4);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    printList(list_test);

    ret = findList(list_test,9,&pos);
    if(SUCCESS != ret) {ADI_PRINT("find err\n");return PARAM_FAIL;}
    ADI_PRINT("ele=9 pos = %d\n",pos);
    
    ret = delList(list_test,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,7);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,9);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;} 
    printList(list_test);

    ret = replaceList(list_test,2,2);
    if(SUCCESS != ret) {ADI_PRINT("change err\n");return PARAM_FAIL;}  
    printList(list_test);  

    
    return SUCCESS;
}



 

双链表:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>  
#include<string.h>
#include<sys/stat.h>
#include<time.h>
#include<stdarg.h>

#if 1
#define INFO_DEBUG    "%d lines in "__FILE__", complie time is "__TIME__" "__DATE__" \n",__LINE__
#define ADI_RUN_ERROR      1
#define ADI_NULL_POINTER  -1
#define ADI_OK             0
#define ADI_PRINT         printf("[%s][%d]:",__FUNCTION__,__LINE__);printf
#define ADI_ASSERT(c,prt,ret)       if(c) {printf(prt);return ret;}
#endif

typedef struct list{
    int elem;
    struct list *prev;
    struct list *next;
}list;


#define PARAM_FAIL -1
#define P_NULL -2
#define SUCCESS 0

/*初始化:只创建头结点*/
list* initList()
{
    list *head = (list*)malloc(sizeof(list));
    if(NULL==head) {printf("NULL");return head;}
    head->prev = NULL;
    head->next = NULL;
    return head;
}

/*pos从0开始,pos=0为第一个*/
int insertList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && NULL != head)//i比head快一个,i=1时p指向的是第0个元素
    {
        head = head->next;
        i++;
    }

    if(NULL != head)
    {
		list *node = (list*)malloc(sizeof(list));
		if(NULL==node) {printf("NULL");return P_NULL;}
		if(NULL != head->next)
		{
			head->next->prev = node;//修改插入位置后一节点的prev指针
		}
		else//插入的是末尾位置,没有后一节点,不需要修该后一节点的prev指针
		{

		}
		node->next = head->next;
		node->prev = head;
		node->elem = elem;
		head->next = node;
		ADI_PRINT("insert elem = %d\n",elem);
    }
    else
    {
       ADI_PRINT("pos err\n");
       return PARAM_FAIL;
    }

    return SUCCESS;
}


/*pos从0开始,pos=0为第一个*/
int replaceList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && NULL != head)//i比head快一个,i=1时head指向的是第0个元素
    {
        head = head->next;
        i++;
    }

    if(NULL != head && NULL != head->next)
    {
		head->next->elem = elem;
		ADI_PRINT("change elem = %d\n",elem);
    }
    else
    {
       ADI_PRINT("pos err\n");
       return PARAM_FAIL;
    }

    return SUCCESS;
}

int delList(list *list_insert,int elem)
{
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;
    while(NULL != head->next)
    {
		if(elem == head->next->elem)
		{
		    list *del = head->next;
		    if(NULL != head->next->next) //如果删除的不是最后一个节点,要修改删除节点后一节点的前向指针
		    {
				head->next->next->prev = head;
		    }
        	head->next = head->next->next;
        	free(del);
        	ADI_PRINT("del ele = %d\n",elem);
        	return SUCCESS;
		}
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",elem);
    return PARAM_FAIL;
}

/*pos为在链表中的位置,ele为待查找的元素*/
int findList(list *listFind,int ele,int *pos)
{
    if(NULL == listFind)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    int i = 0;
    list *head = listFind;
    while(NULL != head->next)
    {
		if(ele == head->next->elem)
		{
        	*pos = i;
        	return SUCCESS;
		}
		i++;
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",ele);
    return PARAM_FAIL;
}

int printList(list *list_prt)
{
    if(NULL == list_prt)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }
    int i = 0;

    ADI_PRINT("head->back print \n");
    while(NULL != list_prt->next)
    { 
        list_prt = list_prt->next;
        ADI_PRINT("list[%d] = %d\n",i,list_prt->elem);
        i++;
    }

    ADI_PRINT("back->head print \n");
    while(NULL != list_prt->prev)
    { 
        ADI_PRINT("list[%d] = %d\n",i,list_prt->elem);
        list_prt = list_prt->prev;
        i--;
    }


    return SUCCESS;
}

int main()
{
    int ret = 0,pos = 0;
    list *list_test = initList();
    if(NULL == list_test)
    {
        ADI_PRINT("err \n");
        return P_NULL;
    }

    ret = insertList(list_test,0,0);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,3,1);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,5,2);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,7,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,9,4);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    printList(list_test);

    ret = findList(list_test,9,&pos);
    if(SUCCESS != ret) {ADI_PRINT("find err\n");return PARAM_FAIL;}
    ADI_PRINT("ele=9 pos = %d\n",pos);
    
    ret = delList(list_test,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,7);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,9);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;} 
    printList(list_test);

    ret = replaceList(list_test,2,2);
    if(SUCCESS != ret) {ADI_PRINT("change err\n");return PARAM_FAIL;}  
    printList(list_test);    

    
    return SUCCESS;
}



双循环链表:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>  
#include<string.h>
#include<sys/stat.h>
#include<time.h>
#include<stdarg.h>

#if 1
#define INFO_DEBUG    "%d lines in "__FILE__", complie time is "__TIME__" "__DATE__" \n",__LINE__
#define ADI_RUN_ERROR      1
#define ADI_NULL_POINTER  -1
#define ADI_OK             0
#define ADI_PRINT         printf("[%s][%d]:",__FUNCTION__,__LINE__);printf
#define ADI_ASSERT(c,prt,ret)       if(c) {printf(prt);return ret;}
#endif

typedef struct list{
    int elem;
    struct list *prev;
    struct list *next;
}list;


#define PARAM_FAIL -1
#define P_NULL -2
#define SUCCESS 0

/*初始化:只创建头结点*/
list* initList()
{
    list *head = (list*)malloc(sizeof(list));
    if(NULL==head) {printf("NULL");return head;}
    head->prev = head;
    head->next = head;
    return head;
}

/*pos从0开始,pos=0为第一个*/
int insertList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && list_insert != head->next)//i比head快一个,i=1时p指向的是第0个元素
    {
        head = head->next;
        i++;
    }

    if((list_insert==head->next)&&(i<pos))//已经到链表尾部,但是pos>i,说明指定的插入位置超过了链表的末尾位置
    {
		return PARAM_FAIL;
    }
    else
    {
		list *node = (list*)malloc(sizeof(list));
		if(NULL==node) {printf("NULL");return P_NULL;}
		node->next = head->next;
		node->prev = head;
		node->elem = elem;
		head->next->prev = node;
		head->next = node;
		ADI_PRINT("insert elem = %d\n",elem);
    }

    return SUCCESS;
}


/*pos从0开始,pos=0为第一个*/
int replaceList(list *list_insert,int elem,int pos)
{
    int i=0;
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;//取头指针
    while(i<pos && list_insert != head->next)
    {
        head = head->next;
        i++;
        //i比head快一个,i=1时head指向的是第0个元素
    }

    if(list_insert==head->next)//已经到链表尾部,说明指定的修改位置超过了链表原有大小
    {
		ADI_PRINT("pos = %d, list's last = %d,change err!\n",pos,i);
		return PARAM_FAIL;
    }
    else
    {
		head->next->elem = elem;
		ADI_PRINT("change elem = %d,pos = %d\n",elem,pos);
    }


    return SUCCESS;
}

int delList(list *list_insert,int elem)
{
    if(NULL == list_insert)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    list *head = list_insert;
    while(list_insert != head->next)
    {
		if(elem == head->next->elem)
		{
		    list *del = head->next;
			head->next->next->prev = head;
        	head->next = head->next->next;
        	free(del);
        	ADI_PRINT("del ele = %d\n",elem);
        	return SUCCESS;
		}
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",elem);
    return PARAM_FAIL;
}

/*pos为在链表中的位置,ele为待查找的元素*/
int findList(list *listFind,int ele,int *pos)
{
    if(NULL == listFind)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }

    int i = 0;
    list *head = listFind;
    while(listFind != head->next)
    {
		if(ele == head->next->elem)
		{
        	*pos = i;
        	return SUCCESS;
		}
		i++;
		head = head->next;
    }

	ADI_PRINT("can not find ele = %d\n",ele);
    return PARAM_FAIL;
}

int printList(list *list_prt)
{
    if(NULL == list_prt)
    {
        ADI_PRINT("err NULL\n");
        return P_NULL;
    }
    int i = 0;

    ADI_PRINT("head->back print \n");
    list *head = list_prt;
    while(list_prt != head->next)
    { 
        head = head->next;
        ADI_PRINT("list[%d] = %d\n",i,head->elem);
        i++;
    }

    ADI_PRINT("back->head print \n");
    while(list_prt != head)
    { 
        i--;
        ADI_PRINT("list[%d] = %d\n",i,head->elem);
        head = head->prev;
        
    }


    return SUCCESS;
}

int main()
{
    int ret = 0,pos = 0;
    list *list_test = initList();
    if(NULL == list_test)
    {
        ADI_PRINT("err \n");
        return P_NULL;
    }

    ret = insertList(list_test,0,0);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,3,1);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,5,2);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,7,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    ret = insertList(list_test,9,4);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}
    printList(list_test);

    ret = findList(list_test,9,&pos);
    if(SUCCESS != ret) {ADI_PRINT("find err\n");return PARAM_FAIL;}
    ADI_PRINT("ele=9 pos = %d\n",pos);
    
    ret = delList(list_test,3);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,7);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;}  
    ret = delList(list_test,9);
    if(SUCCESS != ret) {ADI_PRINT("insert err\n");return PARAM_FAIL;} 
    printList(list_test);

    ret = replaceList(list_test,2,2);
    if(SUCCESS != ret) {ADI_PRINT("change err\n");return PARAM_FAIL;}  
    printList(list_test);    

    
    return SUCCESS;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值