结构体的定义形式:
struct list_node
{
int data ;
};
链表的定义形式
struct list_node
{
int data ;
struct list_node *next ;
};
1.链表和数组 demo1.c
#include <stdio.h>
struct Text
{
int data;
struct Text *next;
};
int main()
{
int i;
int array[] = {1,2,3,4};
for(i=0;i<sizeof(array)/sizeof(array[0]);i++){
printf("%d ",array[i]);
}
putchar('\n');
struct Text t1 = {1,NULL};
struct Text t2 = {2,NULL};
struct Text t3 = {3,NULL};
t1.next = &t2;
t2.next = &t3;
printf("%d %d %d",t1.data,t1.next->data,t1.next->next->data);
return 0;
}
运行结果
2.链表静态添加和动态遍历 demo2.c
#include <stdio.h>
struct Text
{
int data;
struct Text *next;
};
void printLink(struct Text *head)
{
struct Text *point = NULL;
point = head;
while(1){
if(point != NULL){
printf("%d ",point->data);
point = point->next;
}else{
putchar('\n');
break;
}
}
}
int main()
{
int i;
int array[] = {1,2,3,4};
for(i=0;i<sizeof(array)/sizeof(array[0]);i++){
printf("%d ",array[i]);
}
putchar('\n');
struct Text t1 = {1,NULL};
struct Text t2 = {2,NULL};
struct Text t3 = {3,NULL};
struct Text t4 = {5,NULL};
struct Text t5 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
printLink(&t1);
return 0;
}
运行结果 3.统计链表节点的个数以及链表查找 demo3.c
#include <stdio.h>
struct Text
{
int data;
struct Text *next;
};
void printLink(struct Text *head)
{
struct Text *point = NULL;
point = head;
while(1){
if(point != NULL){
printf("%d ",point->data);
point = point->next;
}else{
putchar('\n');
break;
}
}
}
int getTotalNodeNumber(struct Text *head)
{
int cnt = 0;
while(1){
if(head != NULL){
cnt++;
head = head->next;
}else{
break;
}
}
return cnt;
}
int getSearchData(struct Text *head,int data)
{
while(head != NULL){
if(head->data == data){
printf("find data %d\n",head->data);
return 1;
}
head = head->next;
}
return 0;
}
int main()
{
int a;
struct Text t1 = {1,NULL};
struct Text t2 = {2,NULL};
struct Text t3 = {3,NULL};
struct Text t4 = {4,NULL};
struct Text t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
printLink(&t1);
int res = getTotalNodeNumber(&t1);
printf("number=%d\n",res);
printf("input search data\n");
scanf("%d",&a);
int ret = getSearchData(&t1,a);
if(ret == 0){
printf("no find data\n");
}
return 0;
}
4.链表从指定节点后方插入新节点
demo4.c
#include <stdio.h>
struct Text
{
int data;
struct Text *next;
};
int insertNewNoteFromBhind(struct Text *head,int data,struct Text *new)
{
struct Text *p = NULL;
p = head;
while(p != NULL){
if(p->data == data){
new->next = p->next;
p->next = new;
return 1;
}
p = p->next;
}
return 0;
}
void printLink(struct Text *head)
{
struct Text *point = NULL;
point = head;
while(point != NULL){
printf("%d ",point->data);
point = point->next;
}
putchar('\n');
}
int main()
{
int a;
struct Text t1 = {1,NULL};
struct Text t2 = {2,NULL};
struct Text t3 = {3,NULL};
struct Text t4 = {4,NULL};
struct Text t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
struct Text new = {100,NULL};
printLink(&t1);
printf("insert data behind\n");
scanf("%d",&a);
int ret = insertNewNoteFromBhind(&t1,a,&new);
if(ret == 1){
printLink(&t1);
printf("insert data success\n");
}else if(ret == 0){
printf("insert data faile\n");
}
return 0;
}
5.链表从指定节点前插入新节点 在之前插入的话要返回给main函数指针head,才能让链表完整的遍历完,所以要用struct Text *指针的形式返回。
demo5.c
#include <stdio.h>
struct Text
{
int data;
struct Text *next;
};
struct Text *insertNewNoteFromFor(struct Text *head,int data,struct Text *new)
{
struct Text *p = head;
if(p->data == data){
new->next = p;
return new;
}
while(p->next != NULL){
if(p->next->data == data){
new->next = p->next;
p->next = new;
return head;
}
p = p->next;
}
return head;
}
void printLink(struct Text *head)
{
struct Text *point = NULL;
point = head;
while(point != NULL){
printf("%d ",point->data);
point = point->next;
}
putchar('\n');
}
int main()
{
int a;
struct Text t1 = {1,NULL};
struct Text t2 = {2,NULL};
struct Text t3 = {3,NULL};
struct Text t4 = {4,NULL};
struct Text t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
struct Text new2 = {99,NULL};
struct Text *head = &t1;
printLink(head);
printf("insert data forward\n");
scanf("%d",&a);
head = insertNewNoteFromFor(head,a,&new2);
printLink(head);
return 0;
}
运行结果 6.链表删除指定节点
demo6.c
#include <stdio.h>
struct Text
{
int data;
struct Text *next;
};
struct Text *deleteNode(struct Text *head,int data)
{
struct Text *p = head;
if(p->data == data){
head = head->next;
return head;
}
while(p->next != NULL){
if(p->next->data == data){
p->next = p->next->next;
return head;
}
p = p->next;
}
return head;
}
void printLink(struct Text *head)
{
struct Text *point = NULL;
point = head;
while(point != NULL){
printf("%d ",point->data);
point = point->next;
}
putchar('\n');
}
int main()
{
int a;
struct Text t1 = {1,NULL};
struct Text t2 = {2,NULL};
struct Text t3 = {3,NULL};
struct Text t4 = {4,NULL};
struct Text t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
struct Text *head = &t1;
printLink(head);
printf("delete data\n");
scanf("%d",&a);
head = deleteNode(head,a);
printLink(head);
return 0;
}
运行结果 7.链表动态创建头插法 demo7.c
#include <stdio.h>
#include <stdlib.h>
struct Text
{
int data;
struct Text *next;
};
void printLink(struct Text *head)
{
struct Text *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Text *insertNewNoteFor(struct Text *head)
{
struct Text *new;
while(1){
new = (struct Text *)malloc(sizeof(struct Text));
printf("input data until 0\n");
scanf("%d",&new->data);
if(new->data == 0){
printf("quit\n");
return head;
}
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
}
return head;
}
int main()
{
struct Text *head = NULL;
head = insertNewNoteFor(head);
printLink(head);
return 0;
}
运行结果 8.链表动态创建尾插法 demo8.c
#include <stdio.h>
#include <stdlib.h>
struct Text
{
int data;
struct Text *next;
};
void printLink(struct Text *head)
{
struct Text *p = head;
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
}
struct Text *insertNewNoteBehind(struct Text *head,struct Text *new)
{
struct Text *p = head;
if(head == NULL){
head = new;
return head;
}
while(p->next != NULL){
p = p->next;
}
p->next = new;
return head;
}
struct Text *createNote(struct Text *head)
{
struct Text *new;
while(1){
new = (struct Text *)malloc(sizeof(struct Text));
printf("input data until 0\n");
scanf("%d",&new->data);
if(new->data == 0){
printf("quit\n");
return head;
}
head = insertNewNoteBehind(head,new);
}
}
int main()
{
struct Text *head = NULL;
struct Text t1 = {1000,NULL};
head = createNote(head);
printLink(head);
return 0;
}
运行结果
|