简单的无头节点单链表
来源:互联网 发布:ios 判断app网络权限 编辑:程序博客网 时间:2024/04/30 03:07
#include <stdio.h>#include <stdlib.h>typedef struct student{ int num; struct student *next;}student;#define NODE_SIZE sizeof(struct student)/**************************************************** * 函数功能:创建链表 * 函数参数:无 * 函数输出:头指针*****************************************************/static struct student* create_list(){ struct student* head=NULL; /* 指向第一个节点的头指针 */ struct student* p1=NULL; /* 用于新建的节点 */ struct student* p2=NULL; /* 用于指向最后一个节点 */ p1 = (struct student*)malloc(NODE_SIZE); /* 开始时都指向新申请的第一个节点 */ head = p2 = p1; scanf("%d", &p1->num); /* 对新节点数据域进行赋值 */ /* 如果输入不为0,则新建节点 */ while( p1->num != 0) { p1 = (struct student*)malloc(NODE_SIZE); scanf("%d", &p1->num); p2->next = p1; p2 = p1; } p2->next = NULL; return head;}/**************************************************** * 函数功能:遍历链表中每一个数据域 * 函数参数:head头指针 * 函数输出:无*****************************************************/void show_list(struct student* head){ struct student* p = head; while (p != NULL) { printf("%d\n", p->num); p = p ->next; }}/**************************************************** * 函数功能:计算链表的元素个数 * 函数参数:head头指针 * 函数输出:元数个数*****************************************************/int how_maney(struct student** head){ struct student* p = *head; int iCount = 0; while (p!=NULL) { p = p->next; ++iCount; } return iCount;}/**************************************************** * 函数功能:指定位置插入节点 * 函数参数:head头指针,index插入位置,num插入节点的值 * 函数输出:指定位置节点的值*****************************************************/static int insert_list(struct student** head, int index, int num){ int iCount = how_maney(head); /* 新申请的待插入节点 */ struct student* p = (struct student*)malloc(sizeof(struct student)); p->num = num; /* index == 1,则在第一个节点前插入 */ if (index == 1) { p->next = *head; *head = p; return (*head)->num; } else if (index > iCount)/* 说明在链表尾部插入节点 */ { struct student* p1 = *head; while (p1->next != NULL) { p1 = p1->next; } p1->next = p; p->next = NULL; return iCount; } else /* 在中间位置插入新节点 */ { struct student* p1 = *head; int i = 1; while (p1!=NULL && i < index-1) { p1 = p1->next; ++i; } p->next = p1->next; p1->next = p; return i; } return -1;}/**************************************************** * 函数功能:删除指定位置节点 * 函数参数:head头指针,index插入位置,num插入节点的值 * 函数输出:指定位置节点的值*****************************************************/static void delete_list(struct student** head, int index){ struct student* p = *head; struct student* p1 = NULL; int iCount = how_maney(head); /* 首先判断删除是否为头结点 */ if(index == 1) { printf("删除第一个节点\n"); *head = (*head)->next; free(p); return ; } else if (index >= iCount ) /* 如果删除的是尾部节点 */ { int i = 1; printf("删除尾部节点\n"); /* 找到要删除的前一个节点 */ while(p != NULL && i < iCount-1) { p = p->next; i++; } /* 释放节点空间 */ free(p->next); p->next = NULL; return ; } else /* 删除的是中间节点 */ { int i = 1; printf("删除中间节点\n"); while (p!= NULL && i < index-1) { p = p->next; ++i; } p1 = p->next; p->next = p1->next; free(p1); return ; } return ;}int main(){ struct student* head = NULL; struct student* node; head = create_list(node); printf("bbbb\n"); show_list(head); printf("aaaa\n"); printf("链表元素个数:%d\n", how_maney(&head)); /*insert_list(&head, 1, 100); show_list(head); printf("链表元素个数:%d\n", how_maney(&head)); delete_list(&head, 3); show_list(head); printf("链表元素个数:%d\n", how_maney(&head)); */ return 0;}
0 0
- 简单的无头节点单链表
- 单链表 无头节点
- 单链表(无头节点)的实现
- 2.17-比较有头节点的单链表与无头节点插入元素的区别;
- 有/无头节点的单链表的创建(尾插法)
- C语言实现无头节点的单链表
- 【数据结构】单链表(无头节点)
- C语言实现无头节点单链表
- 简单数据结构之链表(无头节点)
- 在无头节点的单链表里删除元素
- 线性表的链式存储结构(无头节点)
- C++双向链表的建立(无头节点)
- 16 - 10 - 31 无头节点 单链表 ------《c primer plus》
- 单链表中的冒泡排序(无头节点)
- 数据实验7倒置无头节点单链表
- 无头节点,删除已知节点的前驱(循环链表)
- 链式存储线性表(无头节点)
- 单向无头节点链表操作
- 商品展示
- easyui datebox添加清空按钮
- XML三种解析方式 SAX、Dom、Pull
- 你不知道XML编程的那些事儿(二)
- 用LSTM进行医疗诊断
- 简单的无头节点单链表
- 逗号表达式
- TI_MotorControlCompendium_2010.PPT 之 Volts-per-Hertz Control
- 坚持#第173天~帮巨人萁成功的重装了系统、辛德勒+珍惜
- hihoCoder 1038 : 01背包
- 学习Linux命令(3)
- 搜索人生之——遗憾
- 【haut 1262: 魔法宝石】+ 暴力
- [LeetCode]300. Longest Increasing Subsequence