单向链表的基本操作

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

typedef struct Student
{
	char name[64];//64
	int id;//4
	char *p;//4
	char**p2;//4
	//struct Student s;//编译不通过 
}Student;

typedef struct Teacher00
{
	char name[64];//64
	int id;//4
	char *p;//4
	char**p2;//4
	//Student s1;//76
	//Student *s2;//4
	struct Teacher00 *t2;//编译可以通过
}Teacher00;

typedef struct Node
{
	int data;
	struct Node *next;
}SLIST;

//创建链表
SLIST* SLIST_create()
{
	SLIST *pHead, *pM,*pCurrent;
	int data;
	pHead = (SLIST *)malloc(sizeof(SLIST));
	if (pHead==NULL)
	{
		return NULL;
	}
	pHead->data = 0;
	pHead->next = NULL;
	pCurrent = pHead;

	printf("\nplease inter your data:");
	scanf("%d",&data);
	while (data!=-1)
	{
		pM = (SLIST *)malloc(sizeof(SLIST));
		if (pM==NULL)
		{
			return NULL;
		}
		pM->data = data;
		pM->next = NULL;
		//新节点如链表
		pCurrent->next = pM;
		pCurrent = pM;
		printf("\nplease inter your data:");
		scanf("%d",&data);
	}
	return pHead;
}
//遍历链表
int SLIST_print(SLIST*pHead)
{
	SLIST *p = pHead->next;
	if (pHead==NULL)
	{
		return -1;
	}
	while (p)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	printf("\n");
	return 0;
}
//插入 在x的前面插入y  若不存在x 在把y插入到最后 不兼容找不到的情况
int SList_insert1(SLIST*pHead,int x,int y)
{
	SLIST*pm,*p;
	if (pHead==NULL)
	{
		return -1;
	}
	p = pHead;
	
	pm = (SLIST*)malloc(sizeof(SLIST));
	if (pm == NULL)
	{
		return -1;
	}
	pm->next = NULL;
	pm->data = y;

	while(p)
	{
		if (p->next->data == x)
		{
			break;
		}
		p = p->next;
	}
	pm->next = p->next;
	p->next =pm;
	return 0;
}
//插入兼容
int SList_insert(SLIST*pHead,int x,int y)
{
	SLIST*pm=NULL,*pcurr=NULL,*pre=NULL;
	if (pHead==NULL)
	{
		return -1;
	}
	pre = pHead;
	pcurr = pHead->next;
	pm = (SLIST*)malloc(sizeof(SLIST));
	if (pm == NULL)
	{
		return -1;
	}
	pm->next = NULL;
	pm->data = y;
	while(pcurr)
	{
		if (pcurr->data == x)
		{
			break;
		}
		pcurr = pcurr->next;
		pre=pre->next;
	}
	pm->next = pre->next;
	pre->next =pm;
	return 0;
}

//删除 data = x的节点
int SList_delete(SLIST*pHead,int x)
{
	SLIST*p=NULL,*tmp=NULL;
	if (pHead==NULL)
	{
		return -1;
	}
	p = pHead->next;
	
	while(p->next)
	{
		if (p->next->data == x)
		{
			tmp = p->next;
			if (p->next->next)
			{
				p->next = p->next->next;
				free(tmp);
			}
			else
			{
				free(p->next);
				p->next=NULL;
			}
			break;
		}
		p = p->next;
	}
	return 0;
}
//链表逆置
int SList_reverse(SLIST*pHead)
{
	SLIST*tmp=NULL,*n=NULL,*p=NULL;

	if (pHead==NULL||pHead->next == NULL||pHead->next->next==NULL)
	{
		return -1;
	}
	p = pHead->next;
	tmp = pHead->next->next;
	while (tmp)
	{
		n = tmp->next;
		tmp->next = p;
		p = tmp;
		tmp = n;
	}
	pHead->next->next = NULL;

	pHead->next = p;

	return 0;
}

//销毁
int SList_destory(SLIST*pHead)
{
	SLIST*p=NULL,*tmp=NULL;

	if (pHead==NULL)
	{
		return -1;
	}
	p = pHead->next;

	while(p)
	{
		tmp = p->next;
		free(p);
		p = tmp;
	}
	return 0;
}


int main()
{
	int ret;
	SLIST *phead =NULL;

	phead = SLIST_create();

	ret = SLIST_print(phead);

	ret = SList_insert(phead,20,19);

	ret = SLIST_print(phead);

	SList_delete(phead,20);

	ret = SLIST_print(phead);

	SList_reverse(phead);

	ret = SLIST_print(phead);
	
	SList_destory(phead);
	
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值