前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >链表基本操作(创建,插入,查找,删除)-C语言

链表基本操作(创建,插入,查找,删除)-C语言

作者头像
星宇大前端
发布2019-01-15 15:43:40
1.8K0
发布2019-01-15 15:43:40
举报
文章被收录于专栏:大宇笔记大宇笔记

     Demo地址:https://github.com/RainManGO/NodeLink

    工具:Xcode

//

// main.c

// Node

//

// Created by apple on 2018/1/3.

// Copyright © 2018年 ZY. All rights reserved.

//

#include <stdio.h>

#include <stdlib.h>

typedef struct Student{

float score;

struct Student * next;

}STU;

#pragma mark 链表的创建

#if 0

//头插入法建表

STU * creat_LinkList(int n)

{

float score;

STU *head, *p;

head = (STU *)malloc(sizeof(STU));

head->next = NULL;

for (int i=0; i<n; i++) {

printf("请输入分数:");

scanf("%f",&score);

p = (STU *)malloc(sizeof(STU));

if (p!=NULL) {

p->score = score;

p->next = head->next;

head->next = p;

}else{

printf("分配内存失败");

}

}

return head;

}

#else

//尾插入建表

STU * creat_LinkList(int n){

float score;

STU * head , *p,*q;

head = p = (STU *)malloc(sizeof(STU));

p->next=NULL;

for (int i=0; i<n; i++) {

printf("请输入分数:");

scanf("%f",&score);

q = (STU *)malloc(sizeof(STU));

if (p!=NULL) {

q->score = score;

q->next=p->next;

p->next = q;

p=q;

}else{

printf("分配内存失败");

}

}

return head;

}

#endif

#pragma mark 链表的查找

//指定个数查找

float getScore(STU * Node,int i){

int j = 1;

STU * p = Node->next;

while (p->next!=NULL&&j<i){

p=p->next;

j++;

};

if (i==j) {

return p->score;

}else{

return 0.f;

}

}

//根据数据值查找节点

STU * getStudent(STU * headLink,float score){

STU * p = headLink;

while (p->next!=NULL||p->score!=score) {

p=p->next;

}

if (p->score==score) {

return p;

}

return NULL;

}

#pragma mark 链表的插入

STU * insertStudent(STU * headLink,int i,float score){

int j=1;

STU * q;

STU * p = headLink->next;

while (p!=NULL &&j<i-1) {

p=p->next;

j++;

}

if (j==i-1) {

q = (STU*)malloc(sizeof(STU));

q->score = score;

q->next=p->next;

p->next=q;

}

return headLink;

}

#pragma mark 链表的删除

//删除第几个节点

STU * deleteStudent(STU * headLink,int i){

int j;

STU *p,*q;

p = headLink;j=0;

q = headLink->next;

while (p->next!=NULL&&j<i) {

p=q;

q=q->next;

j++;

}

if (j==i) {

p->next=q->next;

free(q);

}

return headLink;

}

//按值删除所有节点

STU * deleteStudentByScore(STU * headLink,float score){

STU *p,*q;

p = headLink;

q = headLink->next;

while (p!=NULL) {

if (q->score==score) {

p->next=q->next;

free(q);

q=p->next;

}else{

p=q;

q=q->next;

}

}

return headLink;

}

//删除重复节点

void deleteSameDataNode(STU * headLink){

STU *p,*q,*p1;

p=headLink->next;

while (p->next!=NULL) {

q=p;

p1=p->next;

while (p1->next!=NULL) {

if (p->score==p1->score) {

q->next=p1->next;

free(p1);

p1=q->next;

}else{

q=p1;

p1=p1->next;

}

}

p=p->next;

}

}

void printfLink(STU * headLink){

STU * p = headLink->next;

printf("打印链表如下:\n");

while (p!=NULL) {

printf("%.2f\n",p->score);

p=p->next;

}

printf("\n");

}

int main(int argc, const char * argv[]) {

//创建链表

STU * nodeLink = creat_LinkList(5);

printfLink(nodeLink);

//根据序号查找链表节点值

printf("第%d个链表值为:%.2f\n",3,getScore(nodeLink,3));

//链表值的插入

insertStudent(nodeLink,4,6.6);

printfLink(nodeLink);

//删除链表的值

deleteStudent(nodeLink,1);

printfLink(nodeLink);

//删除重复节点

deleteSameDataNode(nodeLink);

printfLink(nodeLink);

return 0;

}

打印结果:

请输入分数:2

请输入分数:3

请输入分数:3

请输入分数:4

请输入分数:2

打印链表如下:

2.00

3.00

3.00

4.00

2.00

第3个链表值为:3.00

打印链表如下:

2.00

3.00

3.00

6.60

4.00

2.00

打印链表如下:

2.00

3.00

6.60

4.00

2.00

打印链表如下:

2.00

3.00

6.60

4.00

2.00

Program ended with exit code: 0

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年01月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档