尹成老师,关于链表的一些操作(C)

///.h文件

#include <stdio.h>
#include <stdlib.h>






struct student{


int num;
float score;
struct student *pNext;//存储下一个节点的指针


};


typedef struct student ST;


void add(ST **phead, int num, float iscore);
void deleteNode(ST **phead, ST *deletenode);


ST *rev(ST *head);
int getNum(ST *head);
void freeAll(ST *head);
void showall(ST *head);


ST * search(ST*head,int Num);//根据编号查找结点


void change(ST*head, int oldnum, int newnum);


ST *Delete(ST *head, int num);


ST *Headinsert(ST *head, int num,int inum,float iscore);
ST *Endinsert(ST *head, int num, int inum, float iscore);

///  .c                

#include <iostream>
#include <stdlib.h>
#include <string>
#include "linknode.h"


void main()
{
struct student *head = NULL;
add(&head, 1, 70);
add(&head, 2, 80);
add(&head, 3, 70);
add(&head, 4, 80);
add(&head, 5, 70);
/*head = rev(head);*/


/*showall(head);
printf("\nThe Number is of link :%d\n\n\n", getNum(head));
ST *pSearch = search(head, 3);
change(head, 3, 30);
printf("\npSearch = %p, %f\n\n\n", pSearch,pSearch->score);*/
showall(head);
/*head = Delete(head, 6);
showall(head);*/
printf("前插插入之后\n\n\n");
head = Headinsert(head, 4, 13, 100);
showall(head);
printf("后插插入之后\n\n\n");
head = Endinsert(head, 4, 13, 130);
showall(head);
//freeAll(head);
//head = NULL;//一定要注意结点设置为空!!!!
//showall(head);
/*printf("%d,  %f\n", head->num, head->score);
printf("%d,  %f", head->pNext->num, head->pNext->score);
*/
system("pause");
}




void add(ST **phead, int num, float iscore)
{
if (*phead == NULL)//判断链表是否为空
{
ST *newnode = (ST*)malloc(sizeof(ST));//分配内存
if (newnode == NULL)
{
printf("内存分配失败!");
return;
}
newnode->num = num;
newnode->score = iscore;
newnode->pNext = NULL;
*phead = newnode;
}
else
{
//说明链表不为空
ST *p = *phead;
while (p->pNext != NULL)
{
p = p->pNext;//循环向前


}//p=NULL 就终止循环!


ST *newnode = (ST*)malloc(sizeof(ST));//分配内存
newnode->num = num;
newnode->score = iscore;
newnode->pNext = NULL;
p->pNext = newnode;//连接上
}
}
ST *rev(ST *head)
{
ST *p1, *p2, *p3;
p1 = p2 = p3 = NULL;


if (head == NULL||head->pNext == NULL)
{
return head;//返回头结点,在链表为空的的时候和链表只有一个结点的时候,不用翻转!
}
p1 = head;
p2 = head->pNext;


while (p2 != NULL)
{
p3 = p2->pNext;//布局三个结点;
p2->pNext = p1;
p1 = p2;//指针向前移动,从第二个到最后一个结点
p2 = p3;
}
head->pNext = NULL;//代表链表的结束,设置第一个结点为空!
head = p1;
return head;//改变head,并不会生效,需要返回值赋值生效!
}
void deleteNode(ST **phead, ST *deletenode)
{

}
void showall(ST *head)
{
while (head != NULL)
{
printf("num = %d, score =  %f\n", head->num, head->score);
printf("p = %p,   p->pNext = %p\n", head,  head->pNext);
head = head->pNext;
}
}
void freeAll(ST *head)
{


ST *p1, *p2;
p1 = p2 = NULL;
p1 = head;
while (p1->pNext != NULL)
{
p2 = p1->pNext;
p1->pNext = p2->pNext;//p1存储了p1的下一个结点的地址!然后释放
free(p2);
}
free(head);
//
}


int getNum(ST *head)
{
int i = 0;
while (head != NULL)
{
i++;
head = head->pNext;
}


return i;

}




ST * search(ST*head, int Num)
{


while (head != NULL)
{
if (Num == head->num)
{
return head;//返回当前结点指针地址!
}
head = head->pNext;
}


return NULL;
}






void change(ST*head, int oldnum, int newnum)
{


ST *psearch = search(head, oldnum);
if (psearch == NULL)
{
printf("没有找到!");
}
else
{
psearch->num = newnum;
}
}


ST *Delete(ST *head, int num)
{


ST *p1, *p2;
p1 = p2 = NULL;
p1 = head;
while (p1 != NULL )
{
if (p1->num == num)
{
break;
}
else
{
p2 = p1;//记录当前结点,也就是为了找到被删除结点的前一个结点
p1 = p1->pNext;//继续往前
}
}
if (p1 == head)
{
head = p1->pNext;
free(p1);
p1 = NULL;
}
else
{
p2->pNext = p1->pNext;
free(p1);//释放p1;
p1 = NULL;
}


return head;
}




ST *Headinsert(ST *head, int num, int inum, float iscore)
{
ST *p1, *p2;
p1 = p2 = NULL;
p1 = head;
while (p1 != NULL)
{


if (p1->num == num)
{


break;
}
else
{
p2 = p1; //记录当前结点,也就是为了插入数据时候使用,也就是在P2,p1之间插入数据
p1 = p1->pNext;
}
}
if (head == p1)//头结点
{
ST * newNode = (ST*)malloc(sizeof(ST));
newNode->num = inum;
newNode->score = iscore;
newNode->pNext = head;//newnode指向头结点
head = newNode;//newNode成为头结点
}
else
{


ST * newNode = (ST*)malloc(sizeof(ST));
newNode->num = inum;
newNode->score = iscore;
newNode->pNext = p1;//newnode指向头结点
p2->pNext = newNode;//newNode成为头结点
}
return head;
}
ST *Endinsert(ST *head, int num, int inum, float iscore)
{
ST *p1, *p2;
p1 = p2 = NULL;
p1 = head;
while (p1 != NULL)
{
if (p1->num == num)
{


break;
}
else
{
//p2 = p1; //后插的时候不需要记录
p1 = p1->pNext;
}
}
if (p1->pNext == NULL)//最后一个结点
{
ST *newNode = (ST*)malloc(sizeof(ST));
newNode->num = inum;
newNode->score = iscore;
newNode->pNext = NULL;//newnode指向最后的空
p1->pNext = newNode;
}
else
{
p2 = p1->pNext;
ST *newNode = (ST*)malloc(sizeof(ST));
newNode->num = inum;
newNode->score = iscore;
newNode->pNext = p2;//newnode指向最后的空
p1->pNext = newNode;
}
return head;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值