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;
}