c语言单链表练习
#include
#include
#include
typedef struct Node
{
int data;
struct Node *pNext;
}pNode,*ListNode;
//创建结点
struct Node *Create_Node(int dat)
{
struct Node *pHead = (struct Node *)malloc(sizeof(pHead));
//判断是否空间申请成功
if(NULL == pHead)
{
return NULL; //申请失败
}
//先清空间
memset(pHead,' ',sizeof(pHead));
//赋值
pHead->data = dat;
pHead->pNext = NULL;
//返回结点
return pHead;
}
//头插法添加结点
struct Node *InsertNodeByHead(ListNode pHead,ListNode New)
{
New->pNext = pHead->pNext;
pHead->pNext = New;
}
//尾插法添加结点
struct Node *InsertNodeByTail(ListNode pHead,ListNode New)
{
struct Node *Temp = pHead;
while(NULL != Temp->pNext)
{
Temp = Temp->pNext;
}
Temp->pNext = New;
return Temp;
}
//在指定结点之前添加新结点
struct Node *InsertNodeByAppointBefore(ListNode pHead,int index,ListNode New)
{
struct Node *Temp = pHead;
int j = 0;
while((NULL != Temp->pNext) && (j < index -1)) //第一个结点为头结点不存放数据index-1
{
Temp = Temp->pNext;
j++;
}
if(NULL == Temp) //index 超出已有结点下标范围
{
return NULL;
}
//调用头插法函数添加结点
return InsertNodeByHead(Temp,New);
}
//在指定结点之后添加新结点
struct Node *InsertNodeByAppointafter(ListNode pHead,int index,ListNode New)
{
struct Node *Temp = pHead;
int j = 0;
while((NULL != Temp->pNext) && (index -1) > 0)
{
Temp = Temp->pNext;
index--;
}
if(NULL == Temp)
{
return NULL;
}
New->pNext = Temp->pNext;
Temp->pNext = New;
return pHead;
}
//同理根据结点数据前后插入新结点 与上面实现类似
//根据数据查找某个结点
struct Node *FindNodeByData(ListNode pHead,int dat)
{
struct Node *Temp = pHead;
while(NULL != Temp->pNext)
{
Temp = Temp->pNext;
if(Temp->data == dat)
{
return Temp; //找到数据
}
}
return NULL; //循环完还没有找到
}
//删除某个结点数据
int DeleteNodeByData(ListNode pHead,int dat)
{
struct Node *Temp = pHead;
struct Node *PreTemp = NULL;
while(NULL != Temp->pNext)
{
PreTemp = Temp; //保留前一个结点
Temp = Temp->pNext; //结点后移
if(Temp->data == dat) //找到了删除结点
{
//第一种情况删除结点为尾结点
if(NULL == Temp->pNext)
{
PreTemp->pNext = NULL; //前一个结点指向NULL
}
else{
//第二种情况删除结点为中间结点
PreTemp->pNext = Temp->pNext; //前一个结点指向删除结点的下一个
}
free(Temp); //释放删除结点空间
return 1;
}
}
return 0;
}
void PrintNodeData(struct Node *pHead)
{
int num = 1; //记录结点位置
struct Node *Temp = pHead->pNext; //第一个结点开始
while(NULL != Temp)
{
printf("Node %d:%dn",num,Temp->data);
num++; //结点数++
Temp = Temp->pNext; //结点后移
}
}
void NodeBubble(ListNode pHead)
{
struct Node *Temp = pHead->pNext;
struct Node *PreTemp = pHead;
int t;
while(NULL != Temp)
{
PreTemp = Temp->pNext;
while(NULL != PreTemp)
{
if(Temp->data > PreTemp->data)
{
t = Temp->data;
Temp->data = PreTemp->data;
PreTemp->data = t;
}
PreTemp = PreTemp->pNext;
}
Temp = Temp->pNext;
}
}
int main(void)
{
ListNode pH;
pH = Create_Node(0); //第一个结点不存放真实数据
InsertNodeByHead(pH,Create_Node(10));
InsertNodeByHead(pH,Create_Node(40));
InsertNodeByHead(pH,Create_Node(15)); //头插
InsertNodeByTail(pH,Create_Node(6)); //尾插
InsertNodeByTail(pH,Create_Node(5));
InsertNodeByTail(pH,Create_Node(90));
PrintNodeData(pH); //打印
printf("--------------------------------------n");
InsertNodeByAppointBefore(pH,3,Create_Node(100));
printf("--------------结点之前插入------------n");
PrintNodeData(pH);
printf("--------------结点之后插入------------n");
InsertNodeByAppointafter(pH,4,Create_Node(200));
PrintNodeData(pH);
printf("-----------------查找结点-------------n");
ListNode p = FindNodeByData(pH,15);
printf("ret = %d 查找成功n",p->data);
printf("-----------------删除结点-------------n");
DeleteNodeByData(pH,5);
PrintNodeData(pH);
printf("%d 删除成功n",5);
printf("-----------------排序结点-------------n");
NodeBubble(pH);
PrintNodeData(pH);
return 0;
}
运行结果