参考视频:https://www.bilibili.com/video/av35425556?from=search&seid=16538586397705765757
一.创建链表
静态的:
动态的:(创建一个表头表示整个链表)
具体的创建代码
2.创建节点(不创建的话用地址接不上,并且需要把数据放进去)
3.打印节点
注意这里最开始pmove指向的head->next,具体代码如下,并且pmove作为一个指针变量就可以,不需要申请一个空间,只需要通过指向取出值(结构体指针中讲过)
4.插入节点
错误写法
正确示范:
5.链表的指定位置删除(需要注意的就是记录前后两个位置)
所有的代码:
#include<stdio.h> #include<stdlib.h>//malloc的库 struct Node { int data; struct Node* next; }; struct Node* createList() { struct Node* headnode; //将结构体指针变为结构体变量,方法就是申请空间 headnode=(struct Node*)malloc(sizeof(struct Node)); headnode->next=NULL;//初始化别搞忘了,具体的数据可不用 return headnode; } //创建节点 struct Node* createNode(int data) { struct Node* newNode; newNode=(struct Node*)malloc(sizeof(struct Node)); newNode->data=data; newNode->next=NULL; return newNode; } //打印(遍历)节点 void printList(struct Node* headnode) { struct Node* pmove; pmove=headnode->next; while(pmove) { printf("%d",pmove->data); pmove=pmove->next; } } //节点插入(头插法) void insertNodeByHead(struct Node* headNode,int data) { struct Node* newNode=createNode(data); newNode->next=headNode->next; headNode->next=newNode; } //链表删除(通过比对数据,指定位置删除) void deleteNodeByAppoint(struct Node* headNode,int data) { struct Node* posNode=headNode->next; struct Node* posNodeFront=headNode; if(posNode==NULL) { printf("链表为空,无法删除"); } else { while(posNode->data!=data) { posNode=posNode->next; posNodeFront=posNodeFront->next; if(posNode==NULL) { printf("未找到指定元素"); return; } } posNodeFront->next=posNode->next; free(posNode); } } int main() { struct Node* list; list=createList(); insertNodeByHead(list,1); insertNodeByHead(list,2); insertNodeByHead(list,3); printList(list); printf(" "); deleteNodeByAppoint(list,2); printList(list); }
第二种定义链表的方式(参考了队列与栈,将链表的list专门用个结构体表示出来,而不是直接用头结点表示list)
需要注意是:这里申请链表(list)时,还要为头结点申请空间,否则指针会乱指
完整代码:
#include<stdio.h> #include<stdlib.h> typedef struct node { char data; struct node* next; }NODE,*LPNODE; typedef struct list { int size; LPNODE headNode; }LIST,*LPLIST; LPLIST createList() { LPLIST list=(LPLIST)malloc(sizeof(LIST)); LPNODE headNode=(LPNODE)malloc(sizeof(NODE)); list->headNode=headNode; list->headNode->next=NULL;//要留出一个headNode出来,所以是headNode->next=NULL,以后的插入,删除就是统一操作 return list; } LPNODE createNode(char data) { LPNODE newNode=(LPNODE)malloc(sizeof(NODE)); newNode->data=data; newNode->next=NULL; return newNode; } void insertNode(LPLIST list,char data) { LPNODE newNode=createNode(data); newNode->next=list->headNode->next; list->headNode->next=newNode; list->size++; } void printList(LPLIST list) { LPNODE pmove; pmove=list->headNode->next; while(pmove) { printf("%c ",pmove->data); pmove=pmove->next; } } int main() { LPLIST list=createList(); insertNode(list,'A'); insertNode(list,'B'); printList(list); }
结果显示:
可以用结构体定义里面的数据,就能做出来一个简单的学生成绩管理系统,效果图:
实际代码:
#include<stdio.h> #include<stdlib.h>//malloc的库 #include<string.h> struct student { char name[10]; int id; int grade; }; struct Node { struct student data; struct Node* next; }; struct Node* createList() { struct Node* headnode; //将结构体指针变为结构体变量,方法就是申请空间 headnode=(struct Node*)malloc(sizeof(struct Node)); headnode->next=NULL;//初始化别搞忘了,具体的数据可不用 return headnode; } //创建节点 struct Node* createNode(struct student info) { struct Node* newNode; newNode=(struct Node*)malloc(sizeof(struct Node)); newNode->data=info; return newNode; } //打印(遍历)节点 void printList(struct Node* headnode) { struct Node* pmove; pmove=headnode->next; printf("name id grade "); while(pmove) { printf("%s %d %d ",pmove->data.name,pmove->data.id,pmove->data.grade); pmove=pmove->next; } } //节点插入(头插法) void insertNodeByHead(struct Node* headNode,struct student data) { struct Node* newNode=createNode(data); newNode->next=headNode->next; headNode->next=newNode; } //链表删除(通过比对数据,指定位置删除) void deleteNodeByAppoint(struct Node* headNode,int id) { struct Node* posNode=headNode->next; struct Node* posNodeFront=headNode; if(posNode==NULL) { printf("链表为空,无法删除"); } else { while(posNode->data.id!=id) { posNode=posNode->next; posNodeFront=posNodeFront->next; if(posNode==NULL) { printf("未找到指定元素"); return; } } posNodeFront->next=posNode->next; free(posNode); } } int main() { struct student info; char f; struct Node* list; list=createList(); while(1) { printf("Please input name,id,grade:"); scanf("%s%d%d",&info.name,&info.id,&info.grade); insertNodeByHead(list,info); printf("continue?(y/n) "); setbuf(stdin,NULL);//如果不清楚缓存的话,会把回车键读入,程序就会跳过下一句 scanf("%c",&f); if(f=='n'||f=='N') break; } printList(list); }