当前位置:首页 » 编程语言 » c语言链表建立

c语言链表建立

发布时间: 2024-05-06 16:17:33

c语言!!!程序设计:建立一个学生信息链表,包括学号,姓名,成绩.(实现添加,删除,查询,排序,平均)

代码如下:

/*用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=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=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=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=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=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=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=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=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。

以上内容参考:网络-链表

㈡ 用C语言实现建立一个单链表的过程,并实现打印链表中每一个元素,写出完整程序

这是个很简单的链表创建和输出

#include<stdio.h>

#include<stdlib.h>

typedef struct linkednode

{

char data;

struct linkednode *next;

}node,*link_list;//链表节点的结构及重命名

link_list creat()//创建一个链表返回类型是链表的首地址

{

link_list L;

node *p1,*p2;

char data;

L=(node*)malloc(sizeof(node));//开辟存储空间

p2=L;

while((data=getchar())!=' ')//输入回车键时结束输入

{

p1=(node*)malloc(sizeof(node));

p1->data=data;

p2->next=p1;

p2=p1;

}

p2->next=NULL;

return L;

}

void print(link_list L)//把链表输出

{

node *p;

p=L->next;

while(p!=NULL)

{

printf("%c",p->data);

p=p->next;

}

printf(" ");

}

void main()

{

link_list L=NULL;

char x;

printf("请输入链表节点: ");

L=creat();

print(L);

}

㈢ C语言,创建一个链表并赋值1、2、3、4、5,麻烦把全部程序写下

// DLink.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "malloc.h"

typedef struct LNode

{

int data;

struct LNode *next;

}Dlink;

int _tmain(int argc, _TCHAR* argv[])

{

Dlink *l,*s1;

Dlink *s=(Dlink *)malloc(sizeof(Dlink));

l=s;

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

{

s->data=i+1;

s1=(Dlink *)malloc(sizeof(Dlink));

s->next=s1;

s=s1;

}

s->next=NULL;

//数据就保存到以l为头结点的链表中了

return 0;

}

(3)c语言链表建立扩展阅读:

对于非线性的链表,可以参见相关的其他数据结构,例如树、图。另外有一种基于多个线性链表的数据结构:跳表,插入、删除和查找等基本操作的速度可以达到O(nlogn),和平衡二叉树一样。

其中存储数据元素信息的域称作数据域(设域名为data),存储直接后继存储位置的域称为指针域(设域名为next)。指针域中存储的信息又称做指针或链。

由分别表示,,…,的N 个结点依次相链构成的链表,称为线性表的链式存储表示,由于此类链表的每个结点中只包含一个指针域,故又称单链表或线性链表。

㈣ C语言链表的使用方法

D
答案D设置完,p就从链表中丢掉了。
p就是一个指向结构体node的指针。
p->next就是p包含的执行下一个node的指针,在本题,就是q。

热点内容
微信忘记密码从哪里看 发布:2024-05-19 16:06:37 浏览:32
宝马x4贷款买哪个配置好 发布:2024-05-19 15:56:03 浏览:22
微控pid算法 发布:2024-05-19 15:46:31 浏览:135
云盘视频解压密码 发布:2024-05-19 15:23:17 浏览:848
和平精英怎么改地区位置安卓 发布:2024-05-19 15:19:05 浏览:286
酒店的路由器如何配置 发布:2024-05-19 15:10:44 浏览:500
rpgmaker脚本 发布:2024-05-19 14:48:58 浏览:407
hds存储虚拟化 发布:2024-05-19 14:47:09 浏览:21
mysql数据库分片 发布:2024-05-19 14:42:30 浏览:342
2021款魏派vv6买哪个配置 发布:2024-05-19 14:31:11 浏览:633