实现单链表的创建、打印、插入、删除、逆置

实例实现单链表的建立、测长、打印、插入、删除、逆置

<span style="font-size:18px;">//List.h文件

//数据结构的定义
typedef struct student
{
	int data;
	struct student *next;
}node;

typedef node * List;
typedef node * Position;

//编程实现一个单链表的建立
List create();

//编程实现一个单链表的测长
int Length(List L);

//编程实现单链表的打印
void print(List L);

//编程实现单链表删除结点
void del(List L,int num);

//编程实现单链表插入结点
void insert(List L,int num );

//编程实现单链表逆置结点
void reverse(List L);</span>


<span style="font-size:18px;"><span style="background-color: rgb(240, 240, 240);">//List.cpp文件</span>

#include <iostream>
#include <stdio.h>
#include <string>
#include <conio.h>

#include "test.h"
using namespace std;

/************************************************************************/
/* 功能:编程实现一个单链表的建立                                       */
/* 参数:无                                                             */
/* 返回:链表的头结点                                                   */
/************************************************************************/
List create()
{
	Position head, p, s;
	int x,cycle = 1;
	head = (Position)malloc(sizeof(node));  //头结点
	p = head;        //p始终指向新创建的结点
	while(cycle)     //循环参数只要不为0
	{
		printf("please input the data: ");
		scanf("%d",&x);
		if(x != 0)
		{
			s = (Position)malloc(sizeof(node));
			s->data = x;
			printf("\n    %d\n",s->data);
			p->next = s;
			p = s;
		}
		else
			cycle = 0;
	}
	p->next = NULL;
	return head;
}


/************************************************************************/
/* 功能:编程实现一个单链表的测长                                       */
/* 参数:指向链表的头指针                                               */
/* 返回:测量的结果                                                     */
/************************************************************************/
int Length( List L )
{
	int n = 0;
	Position p;
	p = L->next;
	while(p != NULL)
	{
		p = p->next;
		n++;
	}
	return n;
}


/************************************************************************/
/* 功能:编程实现一个单链表的打印                                       */
/* 参数:指向链表的头指针                                               */
/* 返回:无                                                             */
/************************************************************************/
void print( List L )
{
	Position p;
	int n;
	n = Length(L);
	printf("\nNow,These %d Record are:\n",n);
	
	p = L->next;    //L为空着的头结点,因此指向第一个结点

	while(p != NULL)
	{
		printf("\n   %d  \n",p->data);
		p = p->next;
	}
	int a = 0;
}

/************************************************************************/
/* 功能:编程实现删除链表中数据为指定值的结点                           */
/* 参数:L 指向链表的头指针   num:指定删除的数据值                      */
/* 返回:无                                                             */
/************************************************************************/
void del( List L,int num )
{
	List p1,p2;
	p1 = L->next;
	while(num != p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}

	if(num == p1->data)
	{
		if(p1 == L->next)
		{
			L->next = p1->next;
			free(p1);
		}
		else
		{
			p2->next = p1->next;
			free(p1);
		}
	}
	else
		printf("\n%d could not been found",num);
}

/************************************************************************/
/* 功能:编程实现在链表中插入一结点,插入的位置由                       */
/* 参数:L 指向链表的头指针   num:指定删除的数据值                      */
/* 返回:无                                                             */
/************************************************************************/
void insert( List L,int num )
{
	Position p0,p1,p2;
	p1 = L->next;
	p0 = (Position)malloc(sizeof(node));
	p0->data = num;
	while(p0->data > p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}
	if(p0->data <= p1->data)
	{
		if(p1 == L->next)           //在首位插入结点
		{
			p0->next = L->next;
			L->next = p0;
		}
		else                        //在中间插入结点
		{
			p2->next = p0;
			p0->next = p1;
		}

	}
	else                            //在尾部插入节点
	{
		p1->next = p0;
		p0->next = NULL;
	}
}

/************************************************************************/
/* 功能:编程实现对原有链表的逆置                                       */
/* 参数:L 指向链表的头指针                                             */
/* 返回:无                                                             */
/************************************************************************/
void reverse( List L )
{
	List temp = L;
	List Pre = L->next;
	List Cur = Pre->next;
	Pre->next = NULL;            //将原有的头结点也就是逆置后的最后一个结点指向NULL,不再指向原有的头结点 
	List Next;
	free(temp);                  //将原有的头结点内存释放
	while(NULL != Cur)
	{
		Next = Cur->next;
		Cur->next = Pre;
		Pre = Cur;
		Cur = Next;
	}
	//为逆置的链表新建一个新的头结点
	List Head = (List)malloc(sizeof(node));
	Head->next = Pre;
	L = Head;                   //指向原有头结点的指针重新指向逆置后新构建的头结点
}


int main()
{
	List L;
	int n = 0;
	L = create();
	print(L);
	del(L,8);   //这里L无需返回,已经修改
	print(L);
	insert(L,10);
	print(L);
	reverse(L);
	print(L);
	return 0;
}</span>


输出结果:

//创建

please input the data: 2

            2

please input the data: 4

            4

please input the data: 6

            6

please input the data: 8

            8

please input the data: 10

            10

please input the data: 0         

//测长  

Now, These 5 Record are:  

            2

            4

            6

            8

            10

//删除

Now, These 4 Record are:

            2

            4

            6

            10

//插入

Now, These 5 Record are:

            2

            4

            6

            10

            10

//逆置

Now, These 5 Record are:

            10

           10

            6

            4

            2



猜你喜欢

转载自blog.csdn.net/dby3579/article/details/52032849