数据结构带头结点单向不循环链表(C语言版)
生活随笔
收集整理的這篇文章主要介紹了
数据结构带头结点单向不循环链表(C语言版)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
main.c,負(fù)責(zé)測試
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include<stdlib.h> #include "linklist.h" int main() {LNode* list=NULL;int i = 0,err =0;datatype a = 40,return_value=0;datatype arr[] = { 20,10,90,100,50,40,20,60,70,80 };//創(chuàng)建鏈表list = create_list();if (list == NULL){fprintf(stderr, "create_list()failed\n");return -1;}//鏈表中插入數(shù)據(jù),按照從小到大的順序插入for (i = 0; i < sizeof(arr)/sizeof(*arr); i++){//插入失敗,返回-2list_order_insert(list,&arr[i]);}//打印鏈表list_display(list);//按照元素刪除鏈表中data域?yàn)?0的節(jié)點(diǎn)//delete_list(list, &a);//list_display(list);//按位置刪除鏈表中下標(biāo)為3的節(jié)點(diǎn),注意鏈表中有效節(jié)點(diǎn)的下標(biāo)從0開始,retun_value接收返回值/*delete_list_at(list,3,&return_value);printf("刪除節(jié)點(diǎn)的數(shù)據(jù)為%d\n", return_value);list_display(list);*/destroy_list(list); }linklist.c 負(fù)責(zé)具體的代碼實(shí)現(xiàn)
#include<stdio.h> #include<stdlib.h> #include "linklist.h" LNode* create_list() {LNode* ps = NULL;ps=(LNode*)malloc(sizeof(LNode));if (ps == NULL){return -1;} else {ps->next = NULL;return ps;}}//按位置插入節(jié)點(diǎn) int list_insert_at(LNode *ps, int i, datatype* data) {//單向鏈表帶頭結(jié)點(diǎn),第一個有效節(jié)點(diǎn)的下標(biāo)為0int j = 0;LNode* node = ps;LNode* newnode = NULL;if (i < 0){return -1;}//找到第i-1個有效節(jié)點(diǎn)while((j < i)&&(node !=NULL)){node = node->next;j++;}//第i-1個有效節(jié)點(diǎn)存在,生成新節(jié)點(diǎn)if (node){newnode =(LNode*)malloc(sizeof(LNode));if (newnode == NULL){return -2;}newnode->data = *data;newnode->next = node->next;node->next = newnode;return 0;}else {return -3;}}//按從小到大的順序插入元素 int list_order_insert(LNode* ps,datatype *data) {LNode* prenode = ps,*curnode=ps->next,*newnode;while ((curnode != NULL)&&(curnode->data < *data)){prenode = curnode;curnode = curnode->next;}//生成新節(jié)點(diǎn)newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL){return -1;}newnode->data = *data;newnode->next=prenode->next;prenode->next = newnode;return 0; } int list_display(LNode* ps) {int i = 0;if (list_isempty(ps)){return -1;}printf("鏈表中的元素依次為:\n");//找到鏈表中第一個有效節(jié)點(diǎn)ps = ps->next;while (ps){printf("下標(biāo):%d\t數(shù)據(jù):%d\n",i, ps->data);ps = ps->next;i++;}return 0; }int delete_list_at(LNode *ps, int i,datatype *data) {LNode *p = ps,*q=NULL;int j = 0;if (i < 0){return -1;}//找到第i-1個節(jié)點(diǎn)while((p!=NULL) &&(j < i)){p = p->next;j++;}if (p == NULL){return -2;}else{q = p->next;p->next = q->next;*data = q->data;free(q);q = NULL;return 0;} }int delete_list(LNode *ps, datatype *data) {LNode *prenode = ps, *curnode = ps->next;while ((curnode != NULL)&&(curnode->data != *data)){prenode = curnode;curnode = curnode->next; }if (curnode == NULL){return -1;}else {prenode->next = curnode->next;free(curnode);curnode = NULL;return 0;} }int list_isempty(LNode* ps) {if (ps->next){return 0;}else{return 1;} }int destroy_list(LNode* ps) {LNode* newnode, *node = ps->next;//先銷毀有效節(jié)點(diǎn),最后銷毀頭結(jié)點(diǎn)while (node){newnode = node->next;free(node);node = NULL;node = newnode;}//銷毀頭結(jié)點(diǎn)free(ps);ps = NULL;return 0; }linklist.h(頭文件,函數(shù)聲明等)
#ifndef LINKLIST_H__ #define LINKLIST_H__ typedef int datatype; typedef struct LNode {datatype data;struct LNode *next; }LNode; LNode* create_list(); //按從小到大的順序插入元素 int list_order_insert(LNode* ps, datatype *data); //任意位置插入節(jié)點(diǎn) int list_insert_at(LNode *ps, int i,datatype* data); //按元素刪除鏈表中的節(jié)點(diǎn) int delete_list(LNode *ps, datatype *data); //按位置刪除鏈表中的節(jié)點(diǎn),data接收返回值 int delete_list_at(LNode *ps, int i,datatype* data); //打印鏈表中的數(shù)據(jù) int list_display(LNode* ps); //判斷鏈表是否為空 int list_isempty(LNode* ps); //銷毀鏈表 int destroy_list(LNode* ps); #endif總結(jié)
以上是生活随笔為你收集整理的数据结构带头结点单向不循环链表(C语言版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python算法入门
- 下一篇: 用conda安装虚拟的R环境