单链表的头插、尾插、查找、打印、逆置、删除内容是k的节点和求倒数第k个节点;(c实现)

 

代码分为三部分,list.h(头文件)、list.c(函数实现)、test.c(主函数);

list.h

#ifndef _LIST_H_
#define _LIST_H_

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

typedef struct Node
{
	int data;
	struct Node *next;
}Node,*pList;

//初始化
void InitList(pList plist);
//得到一个节点
Node *GetNode(int val);
//头插
void InsertHend(pList list, int val);
//尾插
void InsertTeil(pList list, int vcal);
//查找
void Search(pList plist, int val);
//打印
void Show(pList plist);
//求倒数第k个节点
void Reciprocal(pList plist,int k);
//逆置
void Reverse(pList plist);
//删除是k的节点
void Delete(pList plist, int k);

#endif

 list.c

#include "list.h"

void InitList(pList plist)//头节点
{
	assert(plist != NULL);
	plist->data = -1;
	plist->next = NULL;
}

Node *GetNode(int val)//得到一个新节点
{
	Node *pGet = (Node*)malloc(sizeof(Node));
	assert(pGet != NULL);
	pGet->data = val;
	pGet->next = NULL;
	return pGet;
}

void InsertHend(pList plist, int val)//头插
{
	Node *pGet = GetNode(val);
	assert(plist != NULL);
	pGet->next = plist ->next ;
	plist->next = pGet;
}

void InsertTail(pList plist, int val)//尾插
{
	Node *pGet = GetNode(val);
	assert(plist != NULL);
	while (plist->next != NULL)
	{
		plist = plist->next;
	}
	plist->next = pGet;
}

void Search(pList plist, int val)//查找
{
	assert(plist != NULL);
	while (plist->next != NULL)
	{
		if (((plist->next)->data) == val)
		{
			printf("found %d ,its adress is%p\n", val, &(plist->next));
			break;
		}
		plist = plist->next;
	}
	if (plist->next == NULL && (plist->data != val))
	{
		printf("did not find\n");
	}
}

void Reciprocal(pList plist,int k)//求倒数第k个节点
{
	Node *p = plist->next;
	Node *q = plist->next;
	k = k - 1;
	while (p->next != NULL)
	{
		if (k < 0)
		{
			printf("error\n");
			return 0;
		}
		else if (p->next != NULL && k!=0)
		{
			p = p->next;
			k--;
		}
		else if (k == 0 && p->next != NULL)
		{
			p = p->next;
			q = q->next;
		}
	}
	if (k != 0)
	{
		printf("error\n");
		return 0;
	}
	printf("Countdown to k is %d\n", q->data);
}
void Reverse(pList plist)//逆置
{
	assert(plist != NULL);
	Node *p ,*q;
	p = plist->next;
	plist->next = NULL;
	while ((p->next)!=NULL)
	{
		q = p;
		p = p->next;
		q ->next = plist->next;
		plist->next = q;
	}
	q = p;
	q->next = plist->next;
	plist->next = q;
	printf("reverse success");
}

void Show(pList plist)//打印
{
	assert(plist != NULL);
	Node *p = NULL;
	p = plist->next;
	while (p != NULL)
	{
		printf("%2d", p->data);
		p = p->next;
	}
	printf("\n");
}
void Delete(pList plist, int k)
{
	Node *p, *q;
	p = plist->next;
	q = plist;
	while ((p->next))
	{
		if (p->data == k)
		{
			break;
		}
		p = p->next;
		q = q->next;
	}
	if (p->data == k)
	{
		p = p->next;
		q->next = p;
		printf("delete success");
		return 0;
	}
	else
	{
		p = p->next;
		q = q->next;
		if (p->data == k)
		{
			q->next = NULL;
			printf("delete success");
			return 0;
		}
	}
	printf("The number does not exis\n");
}

test.c

#include "list.h"

int main()
{
	Node head;//头节点

	InitList(&head);

	InsertHend(&head, 1);//添加
	InsertHend(&head, 2);
	InsertHend(&head, 3);
	InsertTail(&head, 4);
	InsertTail(&head, 5);
	InsertTail(&head, 6);
	Search(&head, 4);//查找
	Show(&head);//打印
	Reciprocal(&head,3);//求倒数第k个节点
	Reverse(&head);//逆置
	Show(&head);//打印
	Delete(&head, 5);//删除内容是3的节点
	Show(&head);//打印	
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值