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