C语言链表的建立?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言链表的建立?相关的知识,希望对你有一定的参考价值。

参考以前写的这个吧,写的不好,你修改吧
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define CD sizeof(struct Biao)
struct Biao

int num;
struct Biao *next;
;
int m,x;
int main()

struct Biao *jianli();
void paixu(struct Biao *n);
void chashu(struct Biao *n);
void shanshu(struct Biao *n);
void qiupingjunzhi(struct Biao *n);
void tuichu();
int n;
struct Biao *t;
printf("1.建立链表\n2.排序\n3.插数\n4.删数\n5.求平均值\n");
printf("请输入选项:\n");
for(;;)

scanf("%d",&n);
switch(n)

case 1:t=jianli();break;
case 2:paixu(t);break;
case 3:chashu(t);break;
case 4:shanshu(t);break;
case 5:qiupingjunzhi(t);break;
case 6:tuichu();break;
default:printf("输入错误!请重新输入\n");



struct Biao *jianli()

int i,j;
printf("建立链表,请输入数据:\n");
struct Biao *head,*p1,*p2;
p1=p2=(struct Biao * )malloc(CD);//if(p1==NULL)建立链表失败
for(i=0;i<=100;i++)

if(i==0)

head=p1;
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
continue;

if(p1==NULL)
break;
else

scanf("%d",&p1->num);
if(p1->num==-1)

p2->num=NULL;
break;

p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;


if(head->next->num==NULL)
printf("您建立了一个空链表\n");
else

printf("共有数据:%d\n",i-1);
m=i-1;
printf("输出链表:\n");
p1=head->next;
for(j=0;j<i-1;j++)

printf("%d ",p1->num);
p1=p1->next;


printf("\n");
return(head);

void paixu(struct Biao *n)

int i,j,a,temp;
a=m;
struct Biao *p1,*p2,*tp;
p2=p1=n->next;
printf("从小到大排序结果:\n");
for(i=0;i<a-1;i++)

p1=p2;
tp=p1;
for(j=i+1;j<a;j++)

if((p1->next->num)<(tp->num))

tp=p1->next;

p1=p1->next;

temp=tp->num;tp->num=p2->num;p2->num=temp;
p2=p2->next;

p1=n->next;
for(i=0;i<a;i++)

printf("%d ",p1->num);
p1=p1->next;

printf("\n");

void chashu(struct Biao *n)

int a,i,j;
struct Biao *p1,*p2,*tp;
m=m+1;
x=m;
a=m;
p1=n;
printf("请插入数字:\n");
p2=(struct Biao *)malloc(CD);
scanf("%d",&p2->num);
p1=n->next;
for(i=0;i<a-1;i++)

if(p1->num<=p2->num&&p1->next->num>p2->num)

tp=p1->next;
p1->next=p2;
p2->next=tp;
break;

if(p1->num<p2->num&&p1->next->num>p2->num)

tp=p1->next;
p1->next=p2;
p2->next=tp;
break;

if(n->next->num>p2->num)

tp=n->next;
n->next=p2;
p2->next=tp;
break;

p1=p1->next;

p1=n;//
for(i=0;i<a-1;i++)

p1=p1->next;

if(p1->num<=p2->num)

tp=p1->next;
p1->next=p2;
p2->next=tp;
//算法不简便
printf("插入后的数据:\n");
p1=n->next;
for(i=0;i<a;i++)

printf("%d ",p1->num);
p1=p1->next;

printf("\n");
printf("数据个数:%d\n",a);

void shanshu(struct Biao *n)

int a,i,j;
a=x;
struct Biao *p1,*p2;
printf("请输入要删除的数:\n");
scanf("%d",&j);
for(;;)

p1=n;
for(i=0;i<a;i++)

if(p1->next->num==j)

p2=p1->next;
p1->next=p1->next->next;
a-=1;
x-=1;
break;

p1=p1->next;

if(i==a)
break;

printf("结果:\n");
p1=n->next;
for(i=0;i<a;i++)

printf("%d ",p1->num);
p1=p1->next;

printf("\n");
printf("剩余数据个数:%d\n",x);

void qiupingjunzhi(struct Biao *n)

int s,i;
struct Biao *p1;
s=0;
p1=n->next;
for(i=0;i<x;i++)

s+=p1->num;
p1=p1->next;

printf("平均值为:%f\n",s*1.0/x);

void tuichu()

exit(0);
参考技术A #include<stdio.h>
#include<stdlib.h>

struct chain

int value;
struct chain *next;
;

struct chain *create()

struct chain *head,*tail,*p;
int x;
head = tail = NULL;
while(scanf("%d",&x)==1)

p=(struct chain*)malloc(sizeof(struct chain));
p->value=x;
p->next=NULL;
if(head==NULL)
head = tail = p;
else
tail=tail->next=p;

return head;


struct chain *inlink(struct chain *head,int a,int b) //int a代表要插入的节点,int b代表创建节点的数据域

struct chain *p,*q,*s;
s = (struct chain *)malloc(sizeof(struct chain));
s->value=b;
if(head==NULL)

head = s;
head->next = NULL;

if(head->value == a)

s->next=head;
head = s;

else

p=head;
while((p->value!=a)&&(p->next!=NULL))

q=p;
p=p->next;

if(p->value == a)

q->next = s;
s->next = p;

else

p->next=s;
s->next=NULL;


return (head);


struct chain *dellink(struct chain *head,int a) //int a代表要删除的节点

struct chain *q,*p;
if(head == NULL)
printf("找不到节点!\n");
else if(head->value == a)

p = head;
head = head->next;

else

p=head;
while((p->value!=a)&&(p->next!=NULL))

q=p;
p=p->next;

if(p->value != a)
printf("链表不存在此节点!\n");
else

q->next = p->next;
free(p);


return (head);


void main()

struct chain *p,*q;
q=create(); //链表的创建;
//q=inlink(create(),3,1); //链表的插入;
//q=dellink(create(),2); //链表的删除;
while(q) //输出链表;
printf("%d\n",q->value);
p=q->next;
free(q);
q=p;

本回答被提问者和网友采纳
参考技术B #include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
typedef struct

char sno[10];
char name[10];
float score[3];
Student;

typedef struct node

Student data;
struct node *next;
LinkList;

Student s;
LinkList *head=NULL;
LinkList *last=NULL;

void Create(Student s)

LinkList *p;
p=(LinkList*)malloc(sizeof(LinkList));
p->data=s;
p->next=NULL;
if(head!=NULL)

last->next=p;
last=p;

else

head=p;
last=p;


void OutPut()

LinkList *p;
p=head;
while(p!=NULL)

printf("\n%5s%5s%5.1f%5.1f%5.1f",p->data.sno,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
p=p->next;


void Delete(LinkList *p) //*p 是要删除的节点

LinkList *q;
q=head;
while(q!=NULL && q->next!=p)
q=q->next;
if(q==NULL)printf("\nNode not exist!");
else

printf("删除的节点是:");
printf("\n%5s%5s%5.1f%5.1f%5.1f",p->data.sno,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
q->next=p->next;
free(p);


void Append(Student s)

LinkList *p;
LinkList *q;
p=last; //尾插法

q=(LinkList*)malloc(sizeof(LinkList));
q->data=s;
q->next=p->next;
p->next=q;

void Save()

FILE *fp;
LinkList *p;
p=head;
// char filename="student.txt"
if(fp=fopen("filename","wb")==NULL)printf("\nfile open error");
while(p)

if(fwrite(&(p->data),sizeof(Student),1,fp)!=1)
printf("\nfile write error");
p=p->next;

fclose(fp);

LinkList * Query(Student s)

LinkList *q;
q=head;
while(q!=NULL)

if(strcmp(q->data.sno,s.sno)!=0)
q=q->next;
else break;

return q;

int main()

LinkList *p;
int ch;
do

system("cls");
printf("\n1.创建 2.添加 3.删除 4.查询 5.保存文件 6.输出 0.退出\n");
scanf("%d",&ch);
switch(ch)

case 1:printf("\n请输入:学号,姓名,三门分数\n");
scanf("%s%s%f%f%f",&s.sno,&s.name,&s.score[0],&s.score[1],&s.score[2]);
Create(s);break;
case 2:printf("\n请输入:学号,姓名,三门分数\n");
scanf("%s%s%f%f%f",&s.sno,&s.name,&s.score[0],&s.score[1],&s.score[2]);
Append(s);break;
case 3:printf("\n请输入要删除的学号:"); //按学号删除;
scanf("%s",&s.sno);
p=Query(s);
Delete(p);break;
case 4:printf("\n请输入要查询的学号:"); //按学号查询
scanf("%s",&s.sno);
p=Query(s);
printf("\n%5s%5s%5.1f%5.1f%5.1f",p->data.sno,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
break;
case 5:Save();break;
case 6:OutPut();break;
case 0:exit(0);

printf("\npress any key return to menu...");
getch();
while(1);
return 0;


.....

以上是关于C语言链表的建立?的主要内容,如果未能解决你的问题,请参考以下文章

c语言关于链表的一道题

c语言建立动态链表,我刚学编的程序,请高人帮忙指出毛病

c语言链表的创建

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

c语言对链表的数据排序的问题,分不是问题!

用c语言创建链表