1.链表作业
- 反转链表 通过3个辅助指针变量实现链表的翻转
- 统计链表长度 int size_Linklist(struct LinkNode *pHead);
测试代码:
#include <stdio.h>
#include "linklist.h"
void test02()
{
struct LinkNode *pHead = init_Linklist();
reverse_Linklist(pHead);
printf("reverse_Linklist:\n");
foreach_Linklist(pHead);
printf("Linklist length:%d\n", size_Linklist(pHead));
}
int main() {
test02();
//test01();
return 0;
}
头文件:
//jiedianshengming
struct LinkNode
{
int num;
struct LinkNode *next;
};
void reverse_Linklist(struct LinkNode *pHead);
int size_Linklist(struct LinkNode *pHead);
代码:
#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>
void reverse_Linklist(struct LinkNode *pHead)
{
if(pHead == NULL)
{
return;
}
struct LinkNode *pCurrent = pHead->next;
struct LinkNode *pNext = NULL;
struct LinkNode *pPrve = NULL;
while(pCurrent != NULL)
{
pNext = pCurrent->next;
pCurrent->next = pPrve;
pPrve = pCurrent;
pCurrent = pNext;
}
pHead->next = pPrve;
}
int size_Linklist(struct LinkNode *pHead)
{
if(pHead == NULL)
{
return -1;
}
struct LinkNode *pCurrent = pHead->next;
int num = 0;
while(pCurrent != NULL)
{
num++;
pCurrent = pCurrent->next;
}
return num;
}
结果:
input num, input -1 out
10
input num, input -1 out
20
input num, input -1 out
30
input num, input -1 out
-1
reverse_Linklist:
30
20
10
Linklist length:3
完整代码:
linklist.h
//jiedianshengming
struct LinkNode
{
int num;
struct LinkNode *next;
};
//chushihualianbiao
struct LinkNode *init_Linklist();
void foreach_Linklist(struct LinkNode *pHead);
void insert_Linklist(struct LinkNode *pHead, int oldVal, int newVal);
void delete_Linklist(struct LinkNode *pHead, int val);
void clear_Linklist(struct LinkNode *pHead);
void destroy_Linklist(struct LinkNode *pHead);
void reverse_Linklist(struct LinkNode *pHead);
int size_Linklist(struct LinkNode *pHead);
linklist.c
#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>
struct LinkNode *init_Linklist() {
// chuangjiantoujiedian
struct LinkNode *pHead;
struct LinkNode *pTail;
pHead = (struct LinkNode *)malloc(sizeof(struct LinkNode));
pHead->next = NULL;
pTail = pHead;
if (pHead == NULL) {
return NULL;
}
// chushihua
pHead->next = NULL;
int val = -1;
while (1) {
printf("input num, input -1 out\n");
scanf("%d", &val);
if (val == -1) {
break;
}
// chuangjian xinjiedian
struct LinkNode *newNode;
newNode = (struct LinkNode *)malloc(sizeof(struct LinkNode));
newNode->num = val;
newNode->next = NULL;
// jianliguanxi
pTail->next = newNode;
// genxinweijiedian
pTail = newNode;
}
return pHead;
}
void foreach_Linklist(struct LinkNode *pHead) {
// chuangjian xinlianbiao
struct LinkNode *pCurrent;
pCurrent = pHead->next;
while (pCurrent != NULL) {
printf("%d\n", pCurrent->num);
pCurrent = pCurrent->next;
}
}
void insert_Linklist(struct LinkNode *pHead, int oldVal, int newVal) {
// chuangjian xinjiedain
if (pHead == NULL) {
return;
}
// lianggefuzhizhizhen
struct LinkNode *pPrev = pHead;
struct LinkNode *pCurrent = pHead->next;
while (pCurrent != NULL) {
if (pCurrent->num == oldVal) {
break;
}
// ruguomeizhaodao
pPrev = pCurrent;
pCurrent = pCurrent->next;
}
// chuangjian xinjiedain
struct LinkNode *newNode;
newNode = (struct LinkNode *)malloc(sizeof(struct LinkNode));
newNode->num = newVal;
newNode->next = NULL;
// jianliguanxi
newNode->next = pCurrent;
pPrev->next = newNode;
}
void delete_Linklist(struct LinkNode *pHead, int val) {
if(pHead == NULL)
{
return;
}
// chuangjianlianggefuzhizhizhen
struct LinkNode *pPrev = pHead;
struct LinkNode *pCurrent = pHead->next;
while (pCurrent != NULL) {
// zhaodaole
if (pCurrent->num == val) {
break;
}
// meizhaodao
pPrev = pCurrent;
pCurrent = pCurrent->next;
}
if (pCurrent == NULL) {
return;
}
pPrev->next = pCurrent->next;
// shanchujiedian
free(pCurrent);
pCurrent = NULL;
}
void clear_Linklist(struct LinkNode *pHead)
{
if(pHead == NULL)
{
return;
}
struct LinkNode *pCurrent = pHead->next;
while(pCurrent != NULL)
{
struct LinkNode *Nodenext = pCurrent->next;
free(pCurrent);
pCurrent = Nodenext;
}
//toujiedian de nextzhikong
pHead->next = NULL;
}
void destroy_Linklist(struct LinkNode *pHead)
{
if(pHead == NULL)
{
return;
}
//qingkong
clear_Linklist(pHead);
free(pHead);
pHead = NULL;
}
void reverse_Linklist(struct LinkNode *pHead)
{
if(pHead == NULL)
{
return;
}
struct LinkNode *pCurrent = pHead->next;
struct LinkNode *pNext = NULL;
struct LinkNode *pPrve = NULL;
while(pCurrent != NULL)
{
pNext = pCurrent->next;
pCurrent->next = pPrve;
pPrve = pCurrent;
pCurrent = pNext;
}
pHead->next = pPrve;
}
int size_Linklist(struct LinkNode *pHead)
{
if(pHead == NULL)
{
return -1;
}
struct LinkNode *pCurrent = pHead->next;
int num = 0;
while(pCurrent != NULL)
{
num++;
pCurrent = pCurrent->next;
}
return num;
}
main.c
#include <stdio.h>
#include "linklist.h"
void test01() {
// chushihua
struct LinkNode *pHead = init_Linklist();
// bianli
printf("output linklist:\n");
foreach_Linklist(pHead);
// charu 100 10 200 20 30 300
insert_Linklist(pHead, 10, 100);
insert_Linklist(pHead, 20, 200);
insert_Linklist(pHead, -1, 300);
printf("after linklist:\n");
foreach_Linklist(pHead);
//shanchu 10 200 20 300
delete_Linklist(pHead, 30);
delete_Linklist(pHead, 100);
delete_Linklist(pHead, 1000);
printf("after after linklist:\n");
foreach_Linklist(pHead);
//qingkong
clear_Linklist(pHead);
printf("clear linklist:\n");
foreach_Linklist(pHead);
//ceshilianbiaoshifoucunzai
insert_Linklist(pHead, 111, 111);
insert_Linklist(pHead, 222, 222);
insert_Linklist(pHead, 333, 333);
printf("create the new linklist:\n");
foreach_Linklist(pHead);
destroy_Linklist(pHead);
pHead = NULL;
printf("destroy after:\n");
}
```c
#include <stdio.h>
#include "linklist.h"
void test01() {
// chushihua
struct LinkNode *pHead = init_Linklist();
// bianli
printf("output linklist:\n");
foreach_Linklist(pHead);
// charu 100 10 200 20 30 300
insert_Linklist(pHead, 10, 100);
insert_Linklist(pHead, 20, 200);
insert_Linklist(pHead, -1, 300);
printf("after linklist:\n");
foreach_Linklist(pHead);
//shanchu 10 200 20 300
delete_Linklist(pHead, 30);
delete_Linklist(pHead, 100);
delete_Linklist(pHead, 1000);
printf("after after linklist:\n");
foreach_Linklist(pHead);
//qingkong
clear_Linklist(pHead);
printf("clear linklist:\n");
foreach_Linklist(pHead);
//ceshilianbiaoshifoucunzai
insert_Linklist(pHead, 111, 111);
insert_Linklist(pHead, 222, 222);
insert_Linklist(pHead, 333, 333);
printf("create the new linklist:\n");
foreach_Linklist(pHead);
destroy_Linklist(pHead);
pHead = NULL;
printf("destroy after:\n");
}
void test02()
{
struct LinkNode *pHead = init_Linklist();
reverse_Linklist(pHead);
printf("reverse_Linklist:\n");
foreach_Linklist(pHead);
printf("Linklist length:%d\n", size_Linklist(pHead));
}
int main() {
test02();
//test01();
return 0;
}