单向链表操作

// 链表操作.cpp : Defines the entry point for the console application.
//链表的创建,插入,删除,反转,数据排序~~~~~~~~~~~~

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <windows.h>

typedef struct Node
{
	int value;
	struct Node *pNext;
}NODE, *PNODE;

PNODE create_list(void);
void traverse_list(PNODE pHead);   //带有头结点的输出
void traverse_nofront(PNODE );     //不带头结点的输出
bool insert_list(PNODE pHead, int pos, int val);//在第pos个节点前插入一个新节点,其值为val
bool delete_list(PNODE pHead, int pos, int *pVal);//删除第pos个节点,并通过pVal传回删除的节点值
PNODE reverse(PNODE pHead);
void bubble_sort(PNODE pHead);
int lenght(PNODE pHead);
void swap(int &, int &);
void destory_list(PNODE );

int main(int argc, char* argv[])
{
	int val;
	PNODE pHead = NULL;
	pHead = create_list();

	traverse_list(pHead);

	printf("插入节点后为:\n");
	insert_list(pHead, 3, 93);
	
	traverse_list(pHead);


	//quick_sort(pHead->pNext, NULL);
	traverse_nofront(pHead->pNext);

	if (delete_list(pHead, 3, &val))
	{
		printf("删除第%d个节点%d成功!!",3 , val );
	}
	
	traverse_list(pHead);

	bubble_sort(pHead);
	traverse_list(pHead);

	//带有头结点的操作输出
	//PNODE p = (PNODE)malloc(sizeof(NODE)); //由于返回的链表是没有头节点的,所以在这里需要造一个头结点,而不是只是一个指针就ok
	//p->pNext = reverse(pHead);
	//traverse_list(p);

	//不带头结点的输出
	traverse_nofront(reverse(pHead));

	destory_list(pHead);


	return 0;
}

PNODE create_list(void)
{
	int len, val;
	int i;
	PNODE pHead = (PNODE)malloc(sizeof(NODE));
	if (NULL == pHead)
	{
		printf("内存分配失败!!!!!!!!!\n");
		exit(-1);
	}
	
	PNODE pTail = pHead;
	pTail->pNext = NULL;

	printf("请输入要生成的链表节点个数:len = ");
	scanf("%d", &len);

	srand(time(NULL));
	for (i = 0; i < len; i++)
	{
		val = rand()%100;

		PNODE pNew = (PNODE)malloc(sizeof(NODE));
		if (NULL == pNew)
		{
			printf("内存分配失败!!!\n");
			exit(-1);
		}

		pNew->value = val;
		pTail->pNext = pNew;  //将尾节点的指针域(pTail->pNext)指向新创造的节点,这样实现将创建的节点串接起来~~~~
		pNew->pNext = NULL;
		pTail = pNew;         //可理解为将尾节点移到新建的节点。

	}

	return pHead;
}

void traverse_list(PNODE pHead)
{
	while (NULL != pHead->pNext)
	{
		printf("%d   ", pHead->pNext->value);
		pHead = pHead->pNext;
	}
	printf("\n");
}
void traverse_nofront(PNODE pHead)
{
	PNODE pList = pHead;
    while(pList != NULL)
    {
        printf("%d   ",pList->value);
        pList = pList->pNext;
    }
	printf("\n");
}

bool insert_list(PNODE pHead, int pos, int val)
{
	int i = 0;
	PNODE p = pHead;

	while (NULL != p && i < pos-1)
	{
		p = p->pNext;
		++i;
	}

	if (i > pos-1 || NULL == p)
	{
		printf("插入的节点位置有问题!!\n");
		return false;
	}

	PNODE pNew = (PNODE)malloc(sizeof(NODE));
	if(NULL == pNew)
	{
		printf("内存分配错误!!\n");
		exit(-1);
	}

	pNew->value = val;
	PNODE q = p->pNext;
	p->pNext = pNew;
	pNew->pNext = q;

	return true;
}

bool delete_list(PNODE pHead, int pos, int *pVal)
{
	int i = 0;
	PNODE p = pHead;
	
	while (NULL != p->pNext && i < pos-1)
	{
		p = p->pNext;
		++i;
	}
	
	if (i > pos-1 || NULL == p->pNext)
	{
		printf("删除的节点位置有问题!!\n");
		return false;
	}
	
	PNODE q = p->pNext;
	*pVal = q->value;
	p->pNext = q->pNext;
	free(q);
	q = NULL;

	return true;
}

PNODE  reverse(PNODE pHead)
{
	if (pHead == NULL || pHead->pNext == NULL)
	{
		return pHead;
	}
	PNODE pCurr = pHead->pNext;
	PNODE pPre = NULL;
	PNODE Next = NULL;

	do 
	{
		Next = pCurr->pNext;
		pCurr->pNext = pPre;
		pPre = pCurr;
		pCurr = Next;
	} while (pCurr != NULL);
	printf("Reverse success!!\n");
   return pPre;
}

void bubble_sort(PNODE pHead)
{
	PNODE i;
	PNODE j;
	for ( i = pHead->pNext; i != NULL; i = i->pNext)
		for( j = i->pNext; j != NULL; j = j->pNext)
		{
			if (i->value > j->value)
			{
				swap(i->value, j->value);
			}
		}

	printf("Bubble_sort success!!\n");
}
/*
void quick_sort(PNODE pHead, PNODE pEnd)
{
	if(pHead == NULL ||  pHead == pEnd)  return;

	PNODE pSlow = pHead;
	PNODE pFast = pHead->pNext;
	PNODE pTemp = pHead;

	while (pFast != pEnd)
	{
		if (pFast->value < pHead->value)
		{
			pTemp = pSlow;
			pSlow = pSlow->pNext;
		
			swap(pSlow->value, pFast->value);
		}
		pFast = pFast->pNext;
	}

	swap(pSlow->value, pHead->value);
	quick_sort(pHead, pSlow);
	quick_sort(pSlow->pNext, pEnd);
}
*/
void destory_list(PNODE pHead)
{
	PNODE temp;
	while (pHead != NULL)
	{
		temp = pHead;
		pHead = pHead->pNext;
		free(temp);
	}
	return;
}

int lenght(PNODE pHead)
{
	int len = 0;
	PNODE temp = pHead->pNext;
	while (NULL != temp)
	{
		len++;
		temp = temp->pNext;
	}
	return len;
}

void swap(int &a, int &b)
{
	a = a+b;
	b = a-b;
	a = a-b;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值