C++单链表基本操作练习

vs2012写的

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <malloc.h>

using namespace std;

typedef struct _Node
{
	int iData;
	struct _Node *pNext; 
}NODE,*pNode;                //NODE 是struct node ,*PNode 是struct node * 

pNode CreateThisList(void);
void TraverseThisList(pNode t_node);
bool isEmpty(pNode t_node);
int GetLength(pNode t_node);
void Sort(pNode t_node);
bool InsertElementOne(pNode t_node,int pos,int val);
bool InsertElementTwe(pNode t_node,int i);
bool DeleteElement(pNode t_node,int pos,int &val);

int _tmain(int argc, _TCHAR* argv[])
{
	int i = 0;
	printf("%d\n",i);
	//cout << "hello world" << endl;
	//system(_mm_pause);
	pNode pHead = (pNode)malloc(sizeof(NODE));
	pHead = CreateThisList();
	printf("------------------------------------\n");
	TraverseThisList(pHead);
	if(!isEmpty(pHead))
	{
		printf("不为空\n");
	}else
	{
		printf("是空的\n");
	};

	i  = GetLength(pHead);


	printf("链表长度为:%d\n",i);


	Sort(pHead);

	
	TraverseThisList(pHead);

	if(InsertElementOne(pHead,2,77))
	{
		printf("插入成功\n");
	}
	else
	{
		printf("插入失败\n");
	};

	TraverseThisList(pHead);

	printf("请输入任意值按enter退出\n");
	scanf_s("%d",&i);
	return 0;
}


pNode CreateThisList(void)
{
	int t_length;
	int i = 0;
	int value = 0;
	pNode pHead = (pNode)malloc(sizeof(NODE));
	if(pHead == NULL)
	{
		printf("内存申请失败!\n");
		exit(-1);
	}
	pNode pTail = pHead;
	pTail->pNext = NULL;
	printf("请输入你想创建的链表的大小: ");
	scanf_s("%d",&t_length);
	for(int i = 0;i < t_length;i++)
	{
		printf("请输入一个值: ");
		scanf_s ("%d",&value );
		pNode pNew = (pNode )malloc(sizeof(NODE));
		if(pNew == NULL)
		{
			printf("内存申请失败\n");
			exit(-1);
		}
		pNew->iData = value;
		pTail->pNext = pNew;     //将节点插入
		pNew->pNext = NULL;      //从新设置节点尾部为空
		pTail = pNew;           //蒋新节点送给pTail,使pTail始终指向尾部
	}
	return pHead;
}


void TraverseThisList(pNode t_node)
{
	//pNode tmp = (pNode)malloc(sizeof(NODE));
	/**
#if 0
	if(tmp == NULL)
	{
		printf("申请失败\n");
		exit(-1);
	}
#endif
	**/
	pNode tmp = t_node->pNext;
	printf("--------------------------------------\n");
	while(tmp!=NULL)
	{
		printf("%d ",tmp->iData);
		tmp=tmp->pNext;
	}
	printf("\n");
	return;
}


bool isEmpty(pNode t_node)
{
	if(t_node->pNext == NULL)
	{
		//printf("空的");
		return true;
	}
	else
	{
		return false;
	}
}

int GetLength(pNode t_node)
{
	pNode tmp = t_node->pNext;
	int len = 0;
	while(tmp != NULL)
	{
		len++;
		tmp = tmp->pNext;
	}
	return len;
}

void Sort(pNode t_node)
{
	int len = GetLength(t_node);
	int i,j,t;
	pNode p,q;
	for(i = 0,p = t_node->pNext;i < len;i++,p = p->pNext)
	{
		for(j = i+1,q = p->pNext;j < len;j++,q = q->pNext)
		{
			if(q->iData > p->iData)
			{
				t = p->iData;
				p->iData = q->iData;
				q->iData = t;
			}
		}
	}
	return ;
}

bool InsertElementTwe(pNode t_node,int i)
{
	pNode tmp = (pNode)malloc(sizeof(NODE));
	tmp->iData = i;
	pNode p = t_node->pNext;
	t_node->pNext = tmp;
	tmp->pNext = p;	
//	pTail
	return true;
}

bool InsertElementOne(pNode t_node,int pos,int val)
{
	int i = 0;
	pNode p = t_node;
	while(NULL != p && i < pos - 1)
	{
		p = p->pNext;
		i++;
	}
	if(p == NULL || i > pos -1)
	{
		return false;
	}

	//创建新节点
	pNode pNew = (pNode)malloc(sizeof(NODE));
	if(pNew == NULL)
	{
		printf("分配失败\n");
		exit(-1);
	}

	pNew->iData = val;

	pNode q = p->pNext;
	p->pNext = pNew;
	pNew->pNext = q;
	
	return true;
}

bool DeleteElement(pNode t_node ,int pos,int *val)
{
	int i = 0;
	pNode p = t_node;
	while(NULL != p && i < pos -1)
	{
		p = p->pNext;
		i++;
	}
	if(p == NULL || i > pos - 1)
	{
		//printf("");
		return false;
	}
	//保存要删除的数值
	*val = p->pNext->iData;

	//删除数值
	pNode q = p->pNext;
	p->pNext = p->pNext->pNext;
	free(q);
	q = NULL;
		return true;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

telllong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值