基于Linux操作系统 数据结构 实现双向链表创建、销毁、增删改查操作 C语言

//主函数*********************************************************************************
#include "dlink.h"


int main()
{
	//创建双向链表
	DLink *pLink = createDLink();

	data_t data = {0};
	char cmd;
	int iOffset;

	while(1)
	{
		printf("1  头插 2 中间插  3 尾插 4 show 5 头删\n 6 尾删 7 中间删 8 更新\n");
		cmd = getchar();
		switch(cmd)
		{
		case '1':
			printf("input id:");
			scanf("%d", &data.id);
			printf("input name:");
			scanf("%s", data.name);
			pushItemFront(pLink, data);
			break;
		case '2':
			printf("iOffset:");
			scanf("%d", &iOffset);
			printf("input id:");
			scanf("%d", &data.id);
			printf("input name:");
			scanf("%s", data.name);
			insertItemDLink(pLink, iOffset, data);
			break;
		case '3':
			printf("input id:");
			scanf("%d", &data.id);
			printf("input name:");
			scanf("%s", data.name);
			pushItemBack(pLink, data);
			break;

		case '4':
			showDLink(pLink);
			break;

		case '5':
			popItemFront(pLink);
			break;
		case '6':
			popItemBack(pLink);
			break;
		case '7':
			printf("iOffset:");
			scanf("%d", &iOffset);
			deleteItemDLink(pLink, iOffset);
			break;
		case '8':
		{
			printf("请输入你要更新的name的id号:");
			scanf("%d", &data.id);
			printf("请输入新的名字:");
			scanf("%s", data.name);
			int ret = updateItemDLink(pLink, &data);
			if(NOT_FOUND == ret)
			{
				printf("no this id...\n");
			}
			break;
		}
		default:
			//system("clear");
			break;
		}
		getchar();
	}
	destroyDLink(&pLink);
	return 0;
}

//函数模块功能实现*************************************************************************
#include "dlink.h"

DLink *createDLink()
{
	DLink *pLink = (DLink *)malloc(sizeof(DLink));
	if(NULL == pLink)
	{
		return NULL;
	}
	memset(pLink, 0, sizeof(DLink));
	return pLink;
}


//在第一个节点的前面插入你给的元素
int pushItemFront(DLink *pLink, data_t data)
{
	if(NULL == pLink)
	{
		return DLINK_ERROR;
	}
	//创建一个新的节点
	Node *pNode = createNode(data);

	//插入
	if(pLink->count > 0)
	{
		//因为当插入第一个元素的时候pHead = NULL
		pLink->pHead->pPri = pNode;
	}
	pNode->pNext = pLink->pHead;
	pLink->pHead = pNode;

	pLink->count++;
	return DLINK_OK;
}

Node *createNode(data_t data)
{
	Node *pNode = (Node *)malloc(sizeof(Node));
	if(NULL == pNode)
	{
		return NULL;
	}
	memset(pNode, 0, sizeof(Node));
	pNode->data = data;
	return pNode;
}


void showDLink(DLink *pLink)
{
	if(NULL == pLink)
	{
		return;
	}
	Node *pTmp = pLink->pHead;

	while(pTmp != NULL)
	{
		printf("id:%d name:%s\n", pTmp->data.id, pTmp->data.name);
		pTmp = pTmp->pNext;
	}
}


int insertItemDLink(DLink *pLink, int iOffset, data_t data)
{
	if(NULL == pLink || iOffset < 0 || iOffset > pLink->count)
	{
		return DLINK_ERROR;
	}

	//创建一个新的节点
	Node *pNode = createNode(data);


	if(0 == iOffset)
	{
		pushItemFront(pLink, data);
		return DLINK_OK;
	}

	if(pLink->count == iOffset)
	{
		pushItemBack(pLink, data);
		return DLINK_OK;
	}

	//先找到要插入位置的前一个节点
	Node *pTmp = pLink->pHead;
	int i;
	for(i = 0; i < iOffset-1; i++)
	{
		pTmp = pTmp->pNext;
	}

	pNode->pNext = pTmp->pNext;
	pTmp->pNext->pPri = pNode;
	pTmp->pNext = pNode;
	pNode->pPri = pTmp;

	pLink->count++;
	return DLINK_OK;

}

//尾部插入
int pushItemBack(DLink *pLink, data_t data)
{
	if(NULL == pLink)
	{
		return DLINK_ERROR;
	}

	//创建一个新的节点
	Node *pNode = createNode(data);

	if(0 == pLink->count)
	{
		pushItemFront(pLink, data);
		return DLINK_OK;
	}

	Node *pTmp = pLink->pHead;
	int i;
	for(i = 0; i < pLink->count-1; i++)
	{
		pTmp = pTmp->pNext;
	}

	pTmp->pNext = pNode;
	pNode->pPri = pTmp;
	pLink->count++;
	return DLINK_OK;
}




void destroyDLink(DLink **ppLink)
{
	if(NULL == ppLink || NULL == *ppLink)
	{
		return;
	}

	Node *pDel = (*ppLink)->pHead;

	while(pDel != NULL)
	{
		(*ppLink)->pHead = pDel->pNext;
		free(pDel);
		pDel = (*ppLink)->pHead;
	}
	free(*ppLink);
}


//头部删除
int popItemFront(DLink *pLink)
{
	if(NULL == pLink || 0 == pLink->count)
	{
		return DLINK_ERROR;
	}

	//pDel此时是要删除的首节点
	Node *pDel = pLink->pHead;

	pLink->pHead = pDel->pNext;

	//如果只有一个节点,就不需要给pPri赋值了
	if(1 < pLink->count)
	{
		pLink->pHead->pPri = NULL;
	}

	free(pDel);
	pDel = NULL;

	pLink->count--;
	return DLINK_OK;
}
int popItemBack(DLink *pLink)
{
	if(NULL == pLink || 0 == pLink->count)
	{
		return DLINK_ERROR;
	}

	Node *pTmp = pLink->pHead;
	//如果只有一个节点
	if(1 == pLink->count)
	{
		printf("count = 1...\n");
		free(pLink->pHead);
		pLink->pHead = NULL;
		pLink->count--;
		return DLINK_OK;
	}

	int i;
	for(i = 0; i < pLink->count-2; i++)
	{
		pTmp = pTmp->pNext;
	}
	Node *pDel = pTmp->pNext;
	pTmp->pNext = NULL;
	free(pDel);
	pDel = NULL;
	pLink->count--;
	return DLINK_OK;
}

int deleteItemDLink(DLink *pLink, int iOffset)
{
	if(NULL == pLink || iOffset < 0 || iOffset >= pLink->count || 0 == pLink->count)
	{
		return DLINK_ERROR;
	}

	Node *pTmp = pLink->pHead;

	if(1 == pLink->count || 0 == iOffset)
	{
		popItemFront(pLink);
		return DLINK_OK;
	}
	if(iOffset == pLink->count-1)
	{
		popItemBack(pLink);
		return DLINK_OK;
	}
	int i;
	for(i = 0; i < iOffset-1; i++)
	{
		pTmp = pTmp->pNext;
	}
	Node *pDel = pTmp->pNext;
	pTmp->pNext = pDel->pNext;
	pDel->pNext->pPri = pTmp;
	free(pDel);
	pDel = NULL;
	pLink->count--;
	return DLINK_OK;
}

//更新,main输入要更新的id还有新的name
//循环遍历找到id,更新name

int updateItemDLink(DLink *pLink, data_t *pData)
{
	if(NULL == pLink || NULL == pData)
	{
		return DLINK_ERROR;
	}

	Node *pTmp = pLink->pHead;
	while(pTmp != NULL)
	{
		if(pTmp->data.id == pData->id)
		{
			strcpy(pTmp->data.name, pData->name);
			return DLINK_OK;
		}
		pTmp = pTmp->pNext;
	}
	return NOT_FOUND;
}


//头文件*********************************************************************************


#ifndef _DLINK_H_
#define _DLINK_H_


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

enum DLINK_OP
{
	DLINK_ERROR = -1,
	DLINK_OK = 0,
	NOT_FOUND = 1
};


typedef struct UsrInfo
{
	int id;
	char name[20];
}data_t;


//节点
typedef struct Node
{
	//前一个节点的地址
	struct Node *pPri;
	//数据
	data_t data;
	//后一个节点的地址
	struct Node *pNext;
}Node;

//表结构
typedef struct DLink
{
	//首节点的地址
	Node *pHead;
	//节点个数
	int count;
}DLink;


DLink *createDLink();
int pushItemFront(DLink *pLink, data_t data);
Node *createNode(data_t data);
void showDLink(DLink *pLink);
int insertItemdDIDLink(DLink *pLink, int iOffset, data_t data);
int pushItemBack(DLink *pLink, data_t data);
void destroyDLink(DLink **ppLink);
int popItemFront(DLink *pLink);
int popItemBack(DLink *pLink);
int deleteItemDLink(DLink *pLink, int iOffset);
int updateItemDLink(DLink *pLink, data_t *pData);
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值