单链表之C实现

/**
 * 1、链表是一个结构体
 * 2、链表包含数据和地址两部分
 * 3、单链表每个结点由该结点真实数据和指向下一个结点的指针组成,对于头结点,没有真实数据,对于为节点,地址指针为NULL
 */

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

struct LinkList
{
	int score;
	struct LinkList *next;
};

typedef struct LinkList List;

/**
 * 新加的结点在头部
 */
List *createListInHead(void)
{
	int j = 50;
	List *head;
	List *tamp;
	// 先把链表置空
	head = NULL;

	while (j < 53)
	{
		tamp = (List*) malloc(sizeof(List));
		tamp->score = j;
		tamp->next = head;
		head = tamp;
		j++;
	}

	return head;
}

/**
 * 新加的结点在尾部
 */
List *createListInEnd(void)
{
	int i = 90;
	int listSize = 0;

	List *head;// 链表
	//临时变量,代表下一个结点的指针域
	List *pNextNode;
	List *newNode;// 新的结点

	newNode = pNextNode = (List*)malloc(sizeof(List));
	newNode->score = i;

	head = NULL;// 此时链表是为空的

	while (i < 93)
	{
		if(listSize == 0)
		{
			head = newNode;
		}else
		{
			pNextNode->next = newNode;
		}

		pNextNode = newNode;
		newNode = (List*)malloc(sizeof(List));
		newNode->score = ++i;

		listSize++;
	}
	// 尾结点的指针域要记得赋值为NULL
	pNextNode->next = NULL;

	return head;
}

/**
 * 删除结点
 */
List *delListNode(int score,List *list)
{
	List *newList;

	List *p;

	newList = NULL;
	p = list;

	if(p->score == score)
	{// 要删的结点为头部结点
		printf("----------------删除了头结点-------------------\n");
		newList = p->next;
		return newList;
	}

	while(p != NULL)
	{
		if(p->next != NULL && p->next->score == score)
		{
			printf("----------------删除了结点-------------------\n");
			p->next = p->next->next;
			return list;
		}
		p = p->next;
	}

	printf("----------------没有找到要删除的结点-------------------\n");
	return list;
}

List *insertListNode(List *list,List *insertNode)
{
	List *p;
	List *tamp ;
	tamp = list;

	if(tamp == NULL)
	{
		tamp = insertNode;
		tamp->next = NULL;
		return list;
	}

	if(tamp->score == insertNode->score)
	{
		p = tamp->next;
		tamp->next = insertNode;
		insertNode->next = p;
		return list;
	}

	while(tamp != NULL)
	{
		if(tamp->next != NULL && tamp->next->score == insertNode->score)
		{
			//printf("0000-------------------%d\n",tamp->score);
			p = tamp->next;
			tamp->next = insertNode;
			insertNode->next = p;
			break;
		}else{
			if(tamp->next == NULL)
			{
				//printf("3333-------------------%d\n",tamp->score);
				tamp->next = insertNode;
				insertNode->next = NULL;
				break;
			}
		}
		tamp = tamp->next;
	}

	return list;
}


void printList(List *list)
{
	if(list == NULL)
	{
		printf("This is empty List!!! \n");
		return ;
	}
	List *tamp;
	tamp = list; // 这里tamp指向的是list的第一个数据
	do
	{
		printf("printList score = %d \n", tamp->score);
		tamp = tamp->next;
	} while (tamp != NULL);
}


int main(int argc, char **argv)
{

	printf("****************新加的元素加在链表头部******************\n");
	List *listInHead;
	listInHead = NULL;
	listInHead = createListInHead();
	printList(listInHead);

	printf("****************新加的元素加在链表尾部******************\n");
	List *listInEnd;
	listInEnd = NULL;
	listInEnd = createListInEnd();
	printList(listInEnd);

//	printf("****************删除链表中的结点******************\n");
//	List *newList;
//	newList = NULL;
//	newList = delListNode(99,listInEnd);
//	printList(newList);

	printf("****************insert node into list******************\n");
	List *insertList,*newNode;
	insertList = NULL;
	newNode = (List *)malloc(sizeof(List));
	newNode->score = 95;
	insertList = insertListNode(listInEnd,newNode);
	printList(insertList);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值