# C语言静态链表(指定节点的前后插入新节点与删除)
静态链表相对动态链表,(基于你指针和结构体,内存有清晰的认识的情况下)比较简单,下面是小生的练习代码,提供给大家。
简单的链表样式
#include <stdio.h>
#include <stdlib.h>
struct test
{
int data;
struct test *next;
};
void prinlink(struct test *head)
{
struct test *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
}
int main()
{
struct test t1={1,NULL};
struct test t2={2,NULL};
struct test t8={100,NULL};
struct test t3={3,NULL};
struct test t4={4,NULL};
t1.next = &t2;
t2.next = &t8;
t8.next = &t3;
t3.next = &t4;
prinlink(&t1);
system("pause");
return 0;
}
统计链表节点个数及查找链表
#include <stdio.h>
#include <stdlib.h>
struct test
{
int data;
struct test *next;
};
void prinlink(struct test *head)
{
struct test *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
int chazhao(struct test *head) //查找节点个数
{
int cnt = 0;
struct test *p = head;
while(p != NULL){
cnt++;
p = p->next;
}
return cnt;
}
int chashu(struct test *head,int data) //查找有没有这个点
{
struct test *p = head;
while(p != NULL){
if(p->data == data){
return 1;
}
p = p->next;
}
return 0;
}
int main()
{
struct test t1={1,NULL};
struct test t2={2,NULL};
struct test t3={3,NULL};
struct test t4={4,NULL};
struct test t5={5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
prinlink(&t1);
int ret;
ret = chazhao(&t1);
printf("ret = %d",ret);
putchar('\n');
int pon;
pon = chashu(&t1,3);
if(pon == 1){
printf("have 3");
}else{
printf("no have 3");
}
putchar('\n');
pon = chashu(&t1,9);
if(pon == 1){
printf("have 9");
}else{
printf("no have 9");
}
system("pause");
return 0;
}
链表从指定节点后面插入新节点
#include <stdio.h>
#include <stdlib.h>
struct test
{
int data;
struct test *next;
};
void prinlink(struct test *head)
{
struct test *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
int houm(struct test *head,int data,struct test *new)
{
struct test *p = head;
while(p != NULL){
if(p->data == data){
new->next = p->next;
p->next = new;
return 1;
}
p = p->next;
}
return 0;
}
int main()
{
struct test t1 = {1,NULL};
struct test t2 = {2,NULL};
struct test t3 = {3,NULL};
struct test t4 = {4,NULL};
struct test t5 = {5,NULL};
struct test t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
prinlink(&t1);
putchar('\n');
houm(&t1,3,&t6);
prinlink(&t1);
system("pause");
return 0;
}
链表从指定节点前面插入新节点
#include <stdio.h>
#include <stdlib.h>
struct test
{
int data;
struct test *next;
};
struct test *qianm(struct test *head,int data,struct test *new)
{
struct test *p = head;
if(p->data == data){
new->next = new;
return new;
}
while(p->next != NULL){
if(p->next->data == data){
new->next = p->next;
p->next = new;
return head;
}
p = p->next;
}
return 0;
}
void prinlink(struct test *head)
{
struct test *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
}
int main()
{
struct test *head = NULL;
struct test t1 = {1,NULL};
struct test t2 = {2,NULL};
struct test t3 = {3,NULL};
struct test t4 = {4,NULL};
struct test t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
head = &t1;
struct test new = {999,NULL};
head = qianm(head,5,&new);
prinlink(head);
putchar('\n');
struct test new2 = {555,NULL};
head = qianm(head,999,&new2);
prinlink(head);
system("pause");
return 0;
}
链表中节点的删除
#include <stdio.h>
#include <stdlib.h>
struct test
{
int data;
struct test *next;
};
void prinlink(struct test *head)
{
struct test *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
}
struct test *shanchu(struct test *head,int data)
{
struct test *p = head;
if(p->data == data){
head = head -> next;
free(p);
return head;
}
while(p->next != NULL){
if(p->next->data == data){
p->next = p->next->next;
return head;
}
p = p->next;
}
return 0;
}
int main()
{
struct test *head = NULL;
struct test t1 = {1,NULL};
struct test t2 = {2,NULL};
struct test t3 = {3,NULL};
struct test t4 = {4,NULL};
struct test t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
head = &t1;
prinlink(head);
putchar('\n');
head = shanchu(head,3);
prinlink(head);
putchar('\n');
system("pause");
return 0;
}
小结
函数名起的不是很好,见谅o( ̄▽ ̄)d 。
代码里面有一些小算法,看不懂可以多看几遍o( ̄▽ ̄)d 。