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

c语言编写链表

发布时间: 2023-08-10 02:54:48

Ⅰ 如何用c语言编写一个链表

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

struct Node
{
int data;//数据域
struct Node * next;//指针域
};

/*************************************************************************************
*函数名称:Create
*函数功能:创建链表.
*输入:各节点的data
*返回值:指针head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函数名称:insert
*函数功能:在链表中插入元素.
*输入:head 链表头指针,p新元素插入位置,x 新元素中的数据域内容
*返回值:无
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函数名称:del
*函数功能:删除链表中的元素
*输入:head 链表头指针,p 被删除元素位置
*返回值:被删除元素中的数据域.如果删除失败返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函数名称:print
*函数功能:打印链表中的元素
*输入:head 链表头指针
*返回值:无
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函数名称:main
*函数功能:主函数创建链表并打印链表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}

Ⅱ 怎样创建一个线性链表(C语言)

/*线性链表的构建*/
#include<stdio.h>
#include<stdlib.h>

typedefstructLnode
{
intdata;
structLnode*next;
}Lnode;

intmain()
{
Lnode*H,*p1,*p2,*p3,*p4;
H=(Lnode*)malloc(sizeof(Lnode));
p1=(Lnode*)malloc(sizeof(Lnode));
p2=(Lnode*)malloc(sizeof(Lnode));
p3=(Lnode*)malloc(sizeof(Lnode));
p4=(Lnode*)malloc(sizeof(Lnode));

p1->data=132;
p1->next=p2;
p2->data=942;
p2->next=p3;
p3->data=158;
p3->next=182;
p4->data=231;
p4->next=NULL;

printf("%d,%d ",p1->data,p3->data);
printf("%d",p1->next->data);

return0;
}

Ⅲ 用C语言编写一个链表

看完你下面的追问 其实 意思是
让你把一个已有的 单链表
变成反向的单链表 对吧

Ⅳ C语言如何创建单链表

C语言创建单链表如下:

#include"stdio.h"

#include"stdlib.h"

#include"malloc.h"

#include "iostream.h"

typedef struct node

{

intdata;

node * next;

}node , * List;

void create(int n)

{

int c;

List s,L;

L=(List)malloc(sizeof(node));

L->next=NULL;

printf("请输入第1个数据:");

scanf("%d",&c);

L->data=c;

for(int i=2;i<=n;i++)

{

s=(List)malloc(sizeof(node));

printf("请输入第%d个数据:",i);

scanf("%d",&c);

s->data=c;

s->next=L;

L->next =s;

}

printf("链表创建成功!");

}

void main()

{

int n;

printf("请你输入链表的个数:");

scanf("%d",&n);

create(n);

}

Ⅳ C语言 关于链表的创建

#include<stdio.h>
#include<stdlib.h>

typedefintelemtype;
typedefstructLnode{
elemtypedata;
Lnode*next;
}Lnode;

Lnode*CreatList(Lnode*Head){
Head=(Lnode*)malloc(sizeof(Lnode));
Head->next=NULL;
Lnode*p=Head;
printf("请输入元素的个数:");
inti,n;
scanf("%d",&n);
for(i=0;i<n;++i){
p->next=(Lnode*)malloc(sizeof(Lnode));
printf("请输入第%d个元素:",i+1);
scanf("%d",&p->next->data);
p=p->next;
}
p->next=NULL;
returnHead;
}

voidAllList(Lnode*head){
Lnode*p=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf(" ");
}

intmain(){
Lnode*head=NULL;
head=CreatList(head);
AllList(head);
return0;
}

Ⅵ 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语言创建一个链表,实现增、删、改、查

#includex0dx0a#includex0dx0a#include x0dx0a//先定义一种student类型,表示一个学生的信息,如下:x0dx0atypedef struct studentx0dx0a{x0dx0aint num; //表示学号x0dx0achar name[30]; //表示姓名x0dx0afloat score; //表示分数x0dx0a}student;x0dx0a//定义一种NODE类型,表示一个结点信息,如下:x0dx0atypedef struct nodex0dx0a{x0dx0astudent st; //表示一个学生的信息x0dx0astruct node *next; //表示一个NODE类型的指针x0dx0a}NODE;x0dx0a//1、写出建立一颂老个带头结点的线性链表的函数,侍樱乱其中每个结点包括学号、姓名、分数三个数据域。函数形式如下:x0dx0aNODE *creat_link(int direction)x0dx0a{x0dx0aNODE *head,*p,*tail;x0dx0aint xh,i=1;x0dx0aif(direction==1) //当direction的值为1时,新建立的结点连到尾部x0dx0a{x0dx0atail=head=(NODE *)malloc(sizeof(NODE));x0dx0ahead->next=NULL;x0dx0aprintf("请输入第%d个学生的学号:",i);x0dx0ascanf("%d",&xh);x0dx0awhile(xh>0) //从键盘临时输入学生情况,当输入的学号非正,则链表建立完毕x0dx0a{x0dx0ap=(NODE *)malloc(sizeof(NODE));x0dx0ap->st.num=xh;x0dx0aprintf("请输入第%d个学生的姓名:",i);x0dx0ascanf("%s",p->st.name);x0dx0aprintf("请输入第%d个学生的成绩:",i);x0dx0ascanf("%f",&p->st.score);x0dx0ap->next=NULL;x0dx0atail->next=p;x0dx0atail=p;x0dx0ai=i+1;x0dx0aprintf("请输入第%d个学生的学号:",i);x0dx0ascanf("%d",&xh);x0dx0a}x0dx0a}x0dx0aelse if(direction==0) //当direction为0时,新建立的结点成为第一个结点x0dx0a{x0dx0ahead=(NODE *)malloc(sizeof(NODE));x0dx0ahead->next=NULL;x0dx0aprintf("请输入第%d个学生的学号:",i);x0dx0ascanf("%d",&xh);x0dx0awhile(xh>0) //从键盘临时输入学生情况,老档当输入的学号非正,则链表建立完毕x0dx0a{x0dx0ap=(NODE *)malloc(sizeof(NODE));x0dx0ap->st.num=xh;x0dx0aprintf("请输入第%d个学生的姓名:",i);x0dx0ascanf("%s",p->st.name);x0dx0aprintf("请输入第%d个学生的成绩:",i);x0dx0ascanf("%f",&p->st.score);x0dx0ap->next=head->next;x0dx0ahead->next=p;x0dx0ai=i+1;x0dx0aprintf("请输入第%d个学生的学号:",i);x0dx0ascanf("%d",&xh);x0dx0a}x0dx0a}x0dx0areturn head;x0dx0a}x0dx0a//2、写出输出上述链表各结点数据域值的函数。该函数对应的函数需要一个形参,表示链表的头指针,形式如下:x0dx0avoid print_link(NODE *head)x0dx0a{x0dx0aNODE *p;x0dx0ap=head->next;x0dx0aprintf("%-10s%-20s%-10s\n","学号","姓名","分数");x0dx0awhile(p!=NULL)x0dx0a{x0dx0aprintf("%-10d%-20s%-10.1f\n",p->st.num,p->st.name,p->st.score);x0dx0ap=p->next;x0dx0a}x0dx0a//该函数能输出head所指的链表的所有结点值,输出形式如下:x0dx0a/*本函数输出线性表sq中所有数据,形式如下:x0dx0a学号 姓名 分数x0dx0a12 张三 234.5x0dx0a18 李四 987.7x0dx0a??? ??? ??.*/x0dx0a}x0dx0a//3、写出在链表中删除结点的函数x0dx0aint del_link(NODE *head,char name[])x0dx0a{x0dx0aNODE *p,*p1;x0dx0ap=head->next;x0dx0ap1=head;x0dx0awhile(p!=NULL)x0dx0a{x0dx0aif(strcmp(p->st.name,name)!=0)x0dx0a{x0dx0ap1=p;x0dx0ap=p->next;x0dx0a}x0dx0aelsex0dx0a{x0dx0abreak;x0dx0a}x0dx0a}x0dx0aif(p!=NULL)x0dx0a{x0dx0ap1->next=p->next;x0dx0afree(p);x0dx0areturn 1;x0dx0a}x0dx0aelsex0dx0a{x0dx0areturn 0;x0dx0a}x0dx0a//删除head所指的链表中,名字为name的结点,删除成功返回1,不成功返回0x0dx0a}x0dx0a//4、写出在链表中插入结点的算法x0dx0aint insert(NODE *head,student x,int wz)x0dx0a{x0dx0aNODE *p=head;x0dx0aint i=0,jg;x0dx0aif(wz<=0)x0dx0a{x0dx0ajg=0;x0dx0a}x0dx0aelsex0dx0a{x0dx0awhile(inext;x0dx0a}x0dx0aif(p==NULL)x0dx0a{x0dx0ajg=0;x0dx0a}x0dx0aif(i=wz-1)x0dx0a{x0dx0a//找到wz前面的节点,p指向它x0dx0aNODE *q;x0dx0aq=(NODE *)malloc(sizeof(NODE));x0dx0aq->st.num=x.num;x0dx0astrcpy(q->st.name,x.name);x0dx0aq->st.score=x.score;x0dx0aq->next=p->next;x0dx0ap->next=q;x0dx0ajg=1;x0dx0a}x0dx0a}x0dx0areturn jg;x0dx0a//该函数能够在wz这个结点之前,插入一个新结点,新结点的数据域为x。插入成功返回1,不成功返回0。x0dx0a}x0dx0a//5、写出主函数,分别调用上面算法所对应的程序,建立链表,并输出链表的值。x0dx0avoid main()x0dx0a{x0dx0aNODE *head; //定义指针变量headx0dx0aint wz; //表示插入位置x0dx0achar xm[30];x0dx0astudent st; //定义一个变量st,用来表示一个学生的信息x0dx0ahead=creat_link(1);x0dx0aprint_link(head); //调用函数建立链表,并把返回值送给head;x0dx0a//调用函数,输出链表中各个结点的值x0dx0a//输入一个学生的有关信息,送给变量st的有关成员x0dx0aprintf("\n\n请输入要插入的位置:");x0dx0ascanf("%d",&wz); //输入wz的值x0dx0aprintf("请输入要插入的学生的学号:");x0dx0ascanf("%d",&st.num);x0dx0aprintf("请输入要插入的学生的姓名:");x0dx0ascanf("%s",st.name);x0dx0aprintf("请输入要插入的学生的成绩:");x0dx0ascanf("%f",&st.score); x0dx0a//调用函数,在链表中把学生st的值作为一个结点插入,如果插入成功,输出新链表x0dx0aif(insert(head,st,wz)==1)x0dx0a{x0dx0aprintf("\n插入成功,新表为:\n");x0dx0aprint_link(head);x0dx0a}x0dx0aelsex0dx0a{x0dx0aprintf("插入不成功");x0dx0a}x0dx0a//调用函数,在链表中删除一个指定结点的值,如果删除成功,输出新链表x0dx0aprintf("\n\n请输入要删除的学生的姓名:");x0dx0agetchar();x0dx0agets(xm);x0dx0aif(del_link(head,xm)==1)x0dx0a{x0dx0aprintf("\n删除成功,新表为:\n");x0dx0aprint_link(head);x0dx0a}x0dx0aelsex0dx0a{x0dx0aprintf("删除不成功");x0dx0a}x0dx0a}

Ⅷ 用C语言编程实现单链表的基本操作

第二个显示为什么?为什么什么东西都没有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//创建单链表,头插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("请一次输入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//链表初始化
ListPrint(head);
//打印单链表数据
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。

热点内容
我的世界服务器卡领地 发布:2025-02-06 08:50:45 浏览:255
我的世界公网ip服务器 发布:2025-02-06 08:46:28 浏览:772
php数组值求和 发布:2025-02-06 08:30:56 浏览:819
java类可以作为 发布:2025-02-06 08:28:54 浏览:412
sql更改列 发布:2025-02-06 08:22:37 浏览:396
创建索引sql 发布:2025-02-06 08:22:29 浏览:235
西门子有密码如何初始化 发布:2025-02-06 08:22:28 浏览:594
EV压缩 发布:2025-02-06 08:21:13 浏览:336
配置氯化锡时为什么要加锡粒 发布:2025-02-06 08:19:33 浏览:64
阿里云服务器存放在哪里 发布:2025-02-06 08:11:15 浏览:156