数据结构无头结点单向不循环链表(C语言版)
生活随笔
收集整理的這篇文章主要介紹了
数据结构无头结点单向不循环链表(C语言版)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
main.c(負責測試)
#include <stdlib.h> #include <stdio.h> #include <time.h> #include "nohead.h" int main() {LNode *list = NULL;struct score_st data[10],mydata,data1;int i = 0;srand((unsigned)time(NULL));//初始化數(shù)組for (i = 0; i < 10; i++){data[i].chinese = rand() % 100;data[i].english = rand() % 100;data[i].math = rand() % 100;data[i].student_id = i;snprintf(data[i].name, 32, "學(xué)生的名字是%d", i);}data1.chinese = rand() % 100;data1.english = rand() % 100;data1.math = rand() % 100;data1.student_id = 100;snprintf(data1.name, 32, "學(xué)生的名字是%d", 100);//打印鏈表中的數(shù)據(jù)list_display(list);//鏈表中插入數(shù)據(jù),頭部插入,無頭節(jié)點單向不循環(huán)鏈表for (i = 0; i < 10; i++){list_insert(&list, &data[i]);}list_display(list);//查找id = 3的學(xué)生信息list_find(list,3,&mydata);printf("%4s %20s %4s %4s %4s\n", "學(xué)號", "姓名", "數(shù)學(xué)", "語文", "外語");printf("%4d %20s %4d %4d %4d\n", mydata.student_id,mydata.name, mydata.math, mydata.chinese,mydata.english);//刪除鏈表中的第一個有效節(jié)點(單向不循環(huán)無頭節(jié)點鏈表)//list_delete(&list);//list_display(list);list_insert_at(&list,0,&data1);list_display(list);list_destroy(list); }nohead.c(負責函數(shù)實現(xiàn))
#include<stdio.h> #include<stdlib.h> #include "nohead.h"int list_insert_at(struct LNode **list,int i, struct score_st *data) {struct LNode *node = *list,*newnode = NULL;int j = 0;if (i < 0){return -1;}if (i == 0){newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL){return -3;}newnode->score = *data;newnode->next = *list;*list = newnode;return 0;}//找到第i-1個有效節(jié)點while ((j < i-1)&&(node!=NULL)){node = node->next;j++;}if (node == NULL){return -2;}newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL){return -3;}newnode->score = *data;newnode->next = node->next;node->next = newnode;return 0; }int list_insert(struct LNode **list, struct score_st *data) {LNode *newnode = NULL;newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL)return -1;newnode->score = *data;newnode->next = *list;*list = newnode; }void list_display(LNode *list) {LNode *ps = list;if (ps == NULL){printf("鏈表為空\n");return -1;}printf("學(xué)生的信息如下:\n");printf("%4s %20s %4s %4s %4s\n", "學(xué)號", "姓名", "數(shù)學(xué)", "語文", "外語");while (ps){printf("%4d %20s %4d %4d %4d\n", ps->score.student_id,ps->score.name, ps->score.math, ps->score.chinese,ps->score.english);ps = ps->next;} }int list_find(LNode *ps,int id,struct score_st *data) {while (ps){if (ps->score.student_id == id){*data = ps->score;return 0;}ps=ps->next;}return -1; }//刪除無頭節(jié)點鏈表中第一個有效節(jié)點 int list_delete(LNode **ps) {LNode* curnode = *ps;if (*ps == NULL){printf("鏈表為空刪除失敗\n");return -1;}(*ps) = curnode->next;free(curnode);return 0; }void list_destroy(LNode *p) {LNode *nextnode = NULL;while (p){nextnode = p->next;free(p);p = nextnode;}}nohead.h(負責函數(shù)聲明)
#ifndef NOHEAD_H__ #define NOHEAD_H__ #define MAX_SIZE 32 //定義結(jié)構(gòu)體類型,保存學(xué)生成績 struct score_st {char name[MAX_SIZE];int student_id;int math;int chinese;int english; }; typedef struct LNode {struct score_st score;struct LNode *next; }LNode; //無頭節(jié)點單向不循環(huán)鏈表,在鏈表頭部插入數(shù)據(jù),第一個有效節(jié)點的下標為0 int list_insert(struct LNode *list, struct score_st *score); //按位置插入節(jié)點 int list_insert_at(struct LNode **list, int i, struct score_st *data); void list_display(LNode *list); int list_delete(LNode **ps); void list_destroy(LNode *list); //查找id==**的結(jié)點 int list_find(LNode *ps, int id,struct score_st *data); #endif總結(jié)
以上是生活随笔為你收集整理的数据结构无头结点单向不循环链表(C语言版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米无线网卡linux驱动下载,Linu
- 下一篇: 原生js读取json文件