C语言单链表怎么插入节点

C语言单链表怎么插入节点,第1张

#include "sll_node.h"  

#include <stdlib.h>  

  

#define FALSE   0  

#define TRUE    1  

  

// insertNode2:把newValue的值插入到递增排序的链表中,正确返回TRUE,错误返回FALSE  

// nextp是指向当前节点指针,最初是头指针  

int insertNode2(Node **nextp, int newValue)  

{  

    Node *newNode // 新节点指针  

    Node *current // 当前节点指针  

  

    current = *nextp // 最初当前节点为nextp指针指向的节点  

    // 查找新插入节点的位置  

    while (current != NULL && current->value < newValue)  

    {  

        nextp = ¤t->next  

        current = current->next  

    }  

  

    // 为新节点分配内存  

    newNode = (Node *)malloc(sizeof(Node))  

    if (newNode == NULL)  

        return FALSE  

    newNode->value = newValue  

  

    // 统一了插入的步骤。即:每次插入,都是前一个指针指向新节点,新节点指向下一个节点  

    *nextp = newNode  

    newNode->next = current  

  

    return TRUE  

}  

main函数

[cpp] view plain copy 

#include <stdio.h>  

#include <stdlib.h>  

#include <time.h>  

#include "sll_node.h"  

  

int insertNode(Node **rootp, int newValue)  

int insertNode2(Node **nextp, int newValue)  

  

int main()  

{  

    srand(time(0))  

  

    Node *head = (Node *)malloc(sizeof(Node))  

    head->next = NULL  

  

    for (int i = 0 i < 5 i++)  

    {  

        int temp = rand() % 50  

        printf("%d\n", temp)  

        //insertNode(&head,temp)  

        insertNode2(&head,temp)  

    }  

      

    Node *p = head->next  

    while (p != NULL)  

    {  

        printf("%d\n", p->value)  

        p = p->next  

    }  

  

    getchar()  

    getchar()  

    return 0  

}

代码如下:

/*用c语言链表编写一个学生信息系统程序,要求输出学生的学号,姓名,性别,学号,姓名,成绩(实现添加,删除,查询,排序,平均)*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <stdlib.h>

using namespace std

const int n=5

/*

* nodeEntry : 节点数据类型

* nodeADT   : 节点结构

* linkADT   : 链表结构

*/

typedef struct Student

{

int num

char name[30]

char sex

float score1//语文

float score2//数学

float score3//英语

//struct Student *next

}Student

typedef struct linkCDT {

nodeADT head

}*linkADT

/*

* InitLink   : 初始化链表

* CreateNode : 创建节点

* AppendLink : 添加数据

*/

nodeADT CreateNode(Student entry) {

nodeADT p=(nodeADT)malloc(sizeof*p)

p->entry=entry,p->next=0

return p

}

/*

SortLink : 排序链表

//按学号排序

void SortLinkID(linkADT link) {

nodeADT pHead,pRear,p,tp

if (!link) return

for (pHead=link->head,pRear=0pHeadpHead=pHead->next) {

for (tp=pHead,p=pHead->nextptp=p,p=p->next)

if (pHead->entry.num>=p->entry.num)

tp->next=p->next,p->next=pHead,pHead=p,p=tp

if (!pRear) link->head=pHead

else pRear->next=pHead

pRear=pHead

}

//按英语成绩排序

void SortLinkEnglish(linkADT link) {

nodeADT pHead,pRear,p,tp

if (!link) return

for (pHead=link->head,pRear=0pHeadpHead=pHead->next) {

for (tp=pHead,p=pHead->nextptp=p,p=p->next)

if (pHead->entry.score3>=p->entry.score3)

tp->next=p->next,p->next=pHead,pHead=p,p=tp

if (!pRear) link->head=pHead

else pRear->next=pHead

pRear=pHead

}

}

//按姓名的字典序进行排序

void SortLinkName(linkADT link) {

nodeADT pHead,pRear,p,tp

if (!link) return

for (pHead=link->head,pRear=0pHeadpHead=pHead->next) {

for (tp=pHead,p=pHead->nextptp=p,p=p->next)

if (pHead->entry.name[0]>=p->entry.name[0])

tp->next=p->next,p->next=pHead,pHead=p,p=tp

if (!pRear) link->head=pHead

else pRear->next=pHead

pRear=pHead

}

}

//按姓名的长度进行排序

void SortLinkNameLength(linkADT link) {

nodeADT pHead,pRear,p,tp

if (!link) return

for (pHead=link->head,pRear=0pHeadpHead=pHead->next) {

for (tp=pHead,p=pHead->nextptp=p,p=p->next)

if (strlen(pHead->entry.name)>=strlen(p->entry.name))

tp->next=p->next,p->next=pHead,pHead=p,p=tp

if (!pRear) link->head=pHead

else pRear->next=pHead

pRear=pHead

}

循环链表是与单链表一样

是一种链式的存储结构,所不同的是,循环链表的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而构成一个环形的链。

循环链表的运算与单链表的运算基本一致。所不同的有以下几点:

1、在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。

2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。

以上内容参考:百度百科-链表


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11903462.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存