单链表

单链表

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define ERROR 0
#define OK 1
#define MAX_QSIZE 5


typedef int elemType;

typedef struct lNode
{
	elemType data;
	struct lNode * next;
}* linkListPtr;

void initList(linkListPtr lPtr)
{
	lPtr = (linkListPtr)malloc(sizeof(lNode));
	if(!lPtr)
	{
		exit(OVERFLOW);
	}
	lPtr->next = NULL;
	//头结点的指针域为空
}


bool listInsert(linkListPtr lPtr, int i, elemType val)
{
	int j = 0;
	linkListPtr s, p = lPtr;
	//结点的位置从 1 开始计数
	while(p && j<i-1)
	{
		j++;
		p = p->next;
	}

	if(!p || j > i-1)
	{
		return ERROR;
	}
	s = (linkListPtr)malloc(sizeof(lNode));
	if(!s)
	{
		exit(OVERFLOW);
	}
	s->data = val;
	s->next = p->next;
	p->next = s;
	return OK;
}


bool deleteList(linkListPtr lPtr, int i, elemType * val)
{
	linkListPtr s, p = lPtr;
	int j = 0;
	while(p && j<i-1)
	{
		p = p->next;
		j++;
	}

	if(!p || j > i-1)
	{
		return ERROR;
	}

	s = p->next;
	p->next = s->next;
	*val = s->data;
	free(s);
	return OK;
}


void listTraverse(linkListPtr lPtr, void(* visit)(elemType))
{
	linkListPtr p = lPtr->next;
	while(p != NULL)
	{
		visit(p->data);
		p = p->next;
	}
	printf("\n");
}


void visit(elemType e)
{
	printf("%d, ", e);
}

void destoryList(linkListPtr lPtr)
{
	linkListPtr p ;
	while(lPtr)
	{
		p = lPtr->next;
		free(lPtr);
		lPtr = p;
	}
}

bool listEmpty(linkListPtr lPtr)
{
	if(lPtr->next == NULL)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void clearList(linkListPtr lPtr)
{
	linkListPtr p = lPtr->next;

	// while(p != NULL)
	// {
	// 	lPtr->next = p->next;
	// 	free(p);
	// 	p = lPtr->next;
	// }

	//或者
	lPtr->next = NULL;
	destoryList(p);
}

int listLength(linkListPtr lPtr)
{
	int i = 0;
	linkListPtr p = lPtr->next;

	while(p)
	{
		i++;
		p = p->next;
	}

	return i;
}

bool getElement(linkListPtr lPtr, int i, elemType * val)
{
	if(listEmpty(lPtr))
	{
		return FALSE;
	}

	linkListPtr p = lPtr->next;
	int j = 0;

	while(p && j<i-1)
	{
		j++;
		p = p->next;
	}

	if(p && j == i-1)
	{
		*val = p->data;
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

int locateElement(linkListPtr lPtr, elemType val)
{
	linkListPtr p = lPtr->next;
	int j = 1;

	while(p)
	{
		if (p->data == val)
		{
			return j;
			/* code */
		}
		else
		{
			j++;
			p = p->next;
		}
	}

	return 0;

}

bool priorElement(linkListPtr lPtr, elemType curVal, elemType *preVal)
{
	linkListPtr p = lPtr;

	while(p && p->next)
	{
		if(p->next->data == curVal)
		{
			*preVal = p->data;
			return OK;
		}
		else
		{
			p = p->next;
		}
	}
	return ERROR;
}

bool nextElement(linkListPtr lPtr, elemType curVal, elemType *nextVal)
{
	linkListPtr p = lPtr->next;

	while(p && p->next)
	{
		if(p->data == curVal)
		{
			*nextVal = p->next->data;
			return OK;
		}
		
		p = p->next;
	}

	return ERROR;
}


int main(int argc, char const *argv[])
{
	linkListPtr lPtr;
	bool bl;
	elemType e;
	elemType preVal;
	elemType nextVal;

	initList(lPtr);

	for(int i=1; i<=10; i++)
	{
		listInsert(lPtr, 1, i);
	}

	listTraverse(lPtr, visit);

	printf("list length is:%d\n", listLength(lPtr));

	bl = getElement(lPtr, 2, &e);
	printf("element 2 is:%d\n", e);
	printf("the location of element 7 is:%d\n", locateElement(lPtr, 7));
	printf("the location of element 70 is:%d\n", locateElement(lPtr, 70));

	bl = priorElement(lPtr, 5, &preVal);
	printf("the prior element of 5 is %d\n", preVal);

	bl = nextElement(lPtr, 5, &nextVal);
	printf("the next element of 5 is %d\n", nextVal);

	bl = deleteList(lPtr, 2, &e);
	printf("delete element e = %d\n", e);
	listTraverse(lPtr, visit);

	printf("Is the list is empty?:%d\n", listEmpty(lPtr));
	clearList(lPtr);
	printf("Is the list is empty?:%d\n", listEmpty(lPtr));

	destoryList(lPtr);
	printf("Success!!!\n");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值