链表的基本操作

链表的基本操作:

链表的创建、遍历、插入、删除、销毁、逆置

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

typedef struct Node{
	int node;
	struct Node *next;
}SList;  

SList *SList_Creat();						//创建链表
int SList_print(SList *PHead);				//遍历链表
int SList_Insert(SList *PHead,int x,int y);	//插入元素
int SList_Delete(SList *PHead,int x);		//删除元素
void SList_Destory(SList *PHead);			//销毁链表
int SList_Reverse(SList *PHead);			//逆置链表

SList *SList_Creat()			
{
	SList *pHead = NULL,*pCur,*pM;
	int data;
	//创建头结点并初始化
	pHead = (SList *)malloc(sizeof(SList));
	if( pHead == NULL )
	{
		return NULL;
	}
	pHead->node = 0;
	pHead->next = NULL;

	//接收用户输入
	printf("\nplease enter your data:");
	scanf("%d",&data);
	pCur = pHead;

	while( data != -1 )
	{
		//1.malloc空间
		pM = (SList *)malloc(sizeof(SList));
		if( pM == NULL )
		{
			return NULL;
		}
		pM->next = NULL;
		pM->node = data;
		//2.新节点入链表
		pCur->next = pM;

		pCur = pM;

		printf("\nplease enter your data:");
		scanf("%d",&data);
	}
	return pHead;
}

int SList_Creat2(SList **myList)			
{
	SList *pHead = NULL,*pCur,*pM;
	int data,ret = 0;

	//创建头结点并初始化
	pHead = (SList *)malloc(sizeof(SList));
	if( pHead == NULL )
	{
		ret = -1;
		goto END;
	}
	pHead->node = 0;
	pHead->next = NULL;

	//接收用户输入
	printf("\nplease enter your data:");
	scanf("%d",&data);
	pCur = pHead;

	while( data != -1 )
	{
		//1.malloc空间
		pM = (SList *)malloc(sizeof(SList));
		if( pM == NULL )
		{
			ret = -2;
			goto END;
		}
		pM->next = NULL;
		pM->node = data;
		//2.新节点入链表
		pCur->next = pM;

		pCur = pM;

		printf("\nplease enter your data:");
		scanf("%d",&data);
	}
END:
	if( ret != 0 )
		SList_Destory(pHead);
	else
		*myList = pHead;
	return 0;
}

int SList_print(SList *PHead)	
{
	int ret = 0;
	SList *tmp = NULL;
	if( PHead == NULL )
	{
		ret = -1;
		printf("Error PHead is NULL %d",ret);
		return ret;
	}
	tmp = PHead->next;

	printf("Begin\t");
	while(tmp != NULL)
	{
		printf("%d\t",tmp->node);
		tmp = tmp->next;
	}
	printf("End\n");
	return 0;
}

int SList_Insert(SList *PHead,int x,int y)
{
	int ret = 0;
	SList *pM = NULL,*pCur = NULL,*pPre = NULL;
	if( PHead ==NULL )
	{
		ret = -1;
		printf("Error SList_Insert PHead is NULL.");
		return ret;
	}

	pM = (SList *)malloc(sizeof(SList));
	if( pM == NULL )
	{
		return NULL;
	}
	pM->node = y;
	pM->next = NULL;

	pPre = PHead;
	pCur = PHead->next;

	while( pCur )
	{
		if(pCur->node == x)
		{
			break;
		}
		else
		{
			pPre = pCur;
			pCur = pCur->next;
		}
	}

	pM->next = pCur;
	pPre->next = pM;

	return 0;
}

int SList_Delete(SList *PHead,int x)
{
	SList *pCur = NULL,*pPre = NULL;

	pPre = PHead;
	pCur = PHead->next;

	while( pCur )
	{
		if(pCur->node == x)
		{
			break;
		}
		else
		{
			pPre = pCur;
			pCur = pCur->next;
		}
	}

	if(pCur == NULL )
	{
		printf("没有找到%d\n",x);
		return -1;
	}
	pPre->next = pCur->next;

	if(pCur != NULL)
	{
		free(pCur);
	}

	return 0;
}

void SList_Destory(SList *PHead)
{
	SList *temp = NULL;

	temp = PHead;
	while(temp)
	{
		temp = PHead->next;
		free(PHead);
		PHead = temp;
	}
}

int SList_Reverse(SList *PHead)
{
	SList *pM = NULL,*pCur = NULL,*pPre = NULL;
	int ret = 0;
	if( PHead == NULL || PHead->next == NULL || PHead->next->next == NULL )
	{
		ret = -1;
		printf("SList_Reverse()链表为空");
		return ret;
	}
	pPre = PHead->next;
	pCur = PHead->next->next;
	while( pCur )
	{
		//逆置
		pM = pCur->next;
		pCur->next = pPre;
		//移动
		pPre = pCur;
		pCur = pM;
	}
	PHead->next->next = NULL;
	PHead->next = pPre;

	return 0;
}

void main()
{
	int ret = 0;
	SList *list = NULL;
	SList *list1 = NULL;
	list = SList_Creat();
	ret = SList_print(list);		

	SList_Creat2(&list1);		
	SList_print(list1);
	ret = SList_Insert(list,12,11);	
	ret = SList_print(list);	

	ret = SList_Delete(list,10);
	ret = SList_print(list);	

	ret = SList_Reverse(list);
	ret = SList_print(list);	

	SList_Destory(list);

	system("pause");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值