大二学习数据结构和算法啦,因为之前用Java语言实现过,所以理解起来还是比较轻松,就是理解指针与结构体的运用。废话不多说,上代码!
初始化
typedef struct Node {
int data;
struct Node *next;
} ListNode; ListNode* InitLink() { //初始化,创建头结点
ListNode *Head = (ListNode*)malloc(sizeof(ListNode));
Head->next = NULL;
return Head;
}
添加
void AddLink(ListNode *Head, int msg) { //添加
ListNode *Tmp = Head, *New;
while(Tmp->next != NULL) {
Tmp = Tmp->next;
}
New = (ListNode*)malloc(sizeof(ListNode));
New->data = msg;
New->next = NULL;
Tmp->next = New;
}
删除(可以按值,也可以按位置)
void Delete(ListNode *Head,int data) {//按值删除
if(Head->next == NULL) {
printf("链表无元素可以删除!");
return;
} ListNode *pre = Head; //保存上一个结点
ListNode *cur = Head->next;//保存当前结点
while(cur != NULL) {
if(cur->data == data) {
pre->next = cur->next;//上一个结点的下一个等于当前结点的下一个结点
free(cur);//释放结点
return;//结束
}
pre = cur;//前一个结点等于当前结点
cur = cur->next;//当前结点等于下一个结点
}
} void DeleteMethod(ListNode *head, int element){//按值删除结点
ListNode *pre = head;
ListNode *cur = head->next;
while (cur != NULL){
if (cur->data == element){
pre->next = cur->next;
free(cur);
return;//结束程序
}
pre = cur;
cur = cur->next;
}
} //index代表删除第几个结点
void DeleteLink(ListNode *Head,int index){//按下标删除
ListNode *Tmp = Head;
ListNode *Old;
int mark = ;
if(index < ){
return;
}
while(mark < index- && Tmp->next != NULL){
Old = Tmp;
Tmp = Tmp->next;
mark++;
}
while(Tmp->next == NULL){
return;
}
Old = Tmp->next;
Tmp->next = Old->next;
free(Old);
}
修改(可以按值,也可以按位置)
void UpdataLink(ListNode *head, int oldata, int newdata){//按值修改数据
ListNode *pre = head;
ListNode *cur = head->next;
while (cur != NULL){
if (cur->data == oldata){
cur->data = newdata;
return;//结束程序
}
pre = cur;
cur = cur->next;
}
} void UpLink(ListNode *Head,int index,int element){//按下标修改
ListNode *Tmp = Head;
int mark = ;
if(index < ){
return;
}
while(mark < index - && Tmp->next != NULL){
Tmp = Tmp->next;
mark++;
}
while(Tmp->next == NULL){
return;
}
Tmp->next->data = element;
}
插入(可以按值,也可以按位置)
void InsetrLink(ListNode *head, int oldata, int newdata){//按值输入之前插入
ListNode *New = (ListNode*)malloc(sizeof(ListNode));
New->data = newdata;
New->next = NULL;
ListNode *pre = head;
ListNode *cur = head->next;
while (cur != NULL){
if (cur->data == oldata){
pre->next = New;
New->next = cur;
return;//结束程序
}
pre = cur;
cur = cur->next;
}
}
void InsertLink(ListNode *Head,int index,int element){//按下标插入
ListNode *Tmp = Head;
ListNode *New;
int mark = ;
if(index < ){
return;
}
while(mark < index - && Tmp->next != NULL){
Tmp = Tmp->next;
mark++;
}
while(Tmp->next == NULL){
return;
}
New = (ListNode*)malloc(sizeof(ListNode));
New->data = element;
New->next = Tmp->next;
Tmp->next = New;
}
查询(返回一个结点地址)
//提出查找结点方法,其它方法可以调用
//在单链表中查找第index个结点
ListNode* GetNode(ListNode *Head,int index){//查找结点方法
int mark = ;
ListNode *Tmp = Head;
while(Tmp->next && mark < index){//Tmp->next为NULL 或者 mark==index结束
Tmp = Tmp->next;
mark++;
}
if(index == mark){
return Tmp;
}else{
return NULL;
}
}
销毁
void DestoryLink(ListNode *Head) { //销毁链表
ListNode *Tmp;
while(Head->next){
Tmp = Head->next;
Head->next = Tmp->next;
free(Tmp);
}
free(Head);//头都删掉
}
显示
void Display(ListNode *Head) {//显示
ListNode *Tmp = Head->next;
while(Tmp != NULL) {
printf("%d ",Tmp->data);
Tmp = Tmp->next;
}
}
甩上完整代码
#include<stdio.h>
#include<stdlib.h> //Upon开发————2019.9.26
typedef struct Node {
int data;
struct Node *next;
} ListNode; ListNode* InitLink() { //初始化,创建头结点
ListNode *Head = (ListNode*)malloc(sizeof(ListNode));
Head->next = NULL;
return Head;
} void AddLink(ListNode *Head, int msg) { //添加
ListNode *Tmp = Head, *New;
while(Tmp->next != NULL) {
Tmp = Tmp->next;
}
New = (ListNode*)malloc(sizeof(ListNode));
New->data = msg;
New->next = NULL;
Tmp->next = New;
} void Delete(ListNode *Head,int data) {//按值删除
if(Head->next == NULL) {
printf("链表无元素可以删除!");
return;
} ListNode *pre = Head; //保存上一个结点
ListNode *cur = Head->next;//保存当前结点
while(cur != NULL) {
if(cur->data == data) {
pre->next = cur->next;//上一个结点的下一个等于当前结点的下一个结点
free(cur);//释放结点
return;//结束
}
pre = cur;//前一个结点等于当前结点
cur = cur->next;//当前结点等于下一个结点
}
} void DeleteMethod(ListNode *head, int element){//按值删除结点
ListNode *pre = head;
ListNode *cur = head->next;
while (cur != NULL){
if (cur->data == element){
pre->next = cur->next;
free(cur);
return;//结束程序
}
pre = cur;
cur = cur->next;
}
}
void InsetrLink(ListNode *head, int oldata, int newdata){//按值输入之前插入
ListNode *New = (ListNode*)malloc(sizeof(ListNode));
New->data = newdata;
New->next = NULL;
ListNode *pre = head;
ListNode *cur = head->next;
while (cur != NULL){
if (cur->data == oldata){
pre->next = New;
New->next = cur;
return;//结束程序
}
pre = cur;
cur = cur->next;
}
} void UpdataLink(ListNode *head, int oldata, int newdata){//修改数据
ListNode *pre = head;
ListNode *cur = head->next;
while (cur != NULL){
if (cur->data == oldata){
cur->data = newdata;
return;//结束程序
}
pre = cur;
cur = cur->next;
}
}
void InsertLink(ListNode *Head,int index,int element){//按下标插入
ListNode *Tmp = Head;
ListNode *New;
int mark = ;
if(index < ){
return;
}
while(mark < index - && Tmp->next != NULL){
Tmp = Tmp->next;
mark++;
}
while(Tmp->next == NULL){
return;
}
New = (ListNode*)malloc(sizeof(ListNode));
New->data = element;
New->next = Tmp->next;
Tmp->next = New;
}
void UpLink(ListNode *Head,int index,int element){//按下标修改
ListNode *Tmp = Head;
int mark = ;
if(index < ){
return;
}
while(mark < index - && Tmp->next != NULL){
Tmp = Tmp->next;
mark++;
}
while(Tmp->next == NULL){
return;
}
Tmp->next->data = element;
}
//index代表删除第几个结点
void DeleteLink(ListNode *Head,int index){//按下标删除
ListNode *Tmp = Head;
ListNode *Old;
int mark = ;
if(index < ){
return;
}
while(mark < index- && Tmp->next != NULL){
Old = Tmp;
Tmp = Tmp->next;
mark++;
}
while(Tmp->next == NULL){
return;
}
Old = Tmp->next;
Tmp->next = Old->next;
free(Old);
}
//提出查找结点方法,其它方法可以调用
//在单链表中查找第index个结点
ListNode* GetNode(ListNode *Head,int index){//查找结点方法
int mark = ;
ListNode *Tmp = Head;
while(Tmp->next && mark < index){//Tmp->next为NULL 或者 mark==index结束
Tmp = Tmp->next;
mark++;
}
if(index == mark){
return Tmp;
}else{
return NULL;
}
}
void InsertList(ListNode *Head,int data,int index){//按下标插入
ListNode *Tmp = GetNode(Head,index - ),*New;
if(Tmp == NULL){
return;
}
New = (ListNode*)malloc(sizeof(ListNode));
New->data = data;
New->next = Tmp->next;
Tmp->next = New;
}
void DeleteList(ListNode *Head,int index){//按下标删除
ListNode *Tmp = GetNode(Head,index - ),*New;
if(Tmp == NULL || Tmp->next == NULL){
return;
}
New = Tmp->next;
Tmp->next = New->next;
free(New);
}
void Display(ListNode *Head) {//显示
ListNode *Tmp = Head->next;
while(Tmp != NULL) {
printf("%d ",Tmp->data);
Tmp = Tmp->next;
}
} void DestoryLink(ListNode *Head) { //销毁链表
ListNode *Tmp;
while(Head->next){
Tmp = Head->next;
Head->next = Tmp->next;
free(Tmp);
}
free(Head);//头都删掉
} int main() {
ListNode *Head = InitLink();
AddLink(Head,);
AddLink(Head,);
AddLink(Head,);
AddLink(Head,);
AddLink(Head,); UpdataLink(Head,,);
Display(Head);
return ;
}