
2 stu create() creat e是带回一个指针的函数,该指针指向STU数据
stu ( create)() creat e是指向函数的指针,该函数返回一个STU值
判断是返回是什么型的值就看return后面跟了什么型的变量,才决定函数的类型。
3 一个节点包括数据域和指针域 ,链表的数据结构,必须利用指针变量才能实现。只要
保证降下一个节点的地址放到前一个节点的成员link中即可。从这也可以看出链表与数组存取数据
的不同。这里是很灵活的,指针要在实际的数据结构中才能真正的体现出强大,这也是C语言的精华所在,难学的地方。
4 这个可能是编译器的问题 ,不是有错。像在TC中编译只发现#include<malloch>错误,并没有你说的错误,删掉就正确,可能是TC不支持这个库函数。我查了下书,ANSI标准建议在"stdlibh"头文件中包含有关信息,但许多C编译要求用"malloch"而不是"stdlibh"。这个要多上机去调试。
#include
#include
#define len sizeof(struct student)
struct student
{
int num;
int score;
struct studentnext;
};
struct studentcreat(void)
{
struct studenthead,p1,p2;
p1=p2=(struct student)malloc(len);
scanf("%d %d",&p1->num,&p1->score);
int n=0;
while(p1->num!=0)
{
if(n=1)head=p1;
else
{ p1=(struct student)malloc(len);
scanf("%d%d",&p1->num,&p1->score);
p2->next=p1;
}
n++;
}
p1->next=NULL;
return head;
}
main()
{
struct studentpt;
pt=creat();
printf("%d\n%d\n",&pt->num,&pt->score);
}
Oxygene 是一种基于 Object Pascal 的面向对象编程语言,拥有丰富的功能集。它以前被称为 ‘Chrome’。
写好了,你看下
#include <stdioh>
#include <stdlibh>
#include <malloch>
typedef struct node
{
int data;
struct node next;
}Node;
void InitList(Node head);
void CreateList(Node head);
void InsertList(Node head, int key);
void DeleteList(Node head, int key);
void PrintList(Node head);
//初始化链表
void InitList(Node head)
{
(head) = (Node )malloc(sizeof(Node));
(head)->next = NULL;
}
//创建链表
void CreateList(Node head)
{
int i;
printf("您好,请输入您要插入的数据:\n");
scanf("%d", &i);
while(i != 0)
{
InsertList(head, i);
scanf("%d", &i);
}
}
//插入链表
void InsertList(Node head, int key)
{
Node p, q, s;
q = (head);
p = (head)->next;
while(p)
{
q = p;
p = p->next;
}
s = (Node )malloc(sizeof(Node));
s->data = key;
s->next = NULL;
q->next = s;
}
//删除链表
void DeleteList(Node head, int key)
{
Node p, q;
q = (head);
p = (head)->next;
while(p && p->data != key)
{
q = p;
p = p->next;
}
if(p)
{
q->next = p->next;
free(p);
p = NULL;
}
}
//输出链表
void PrintList(Node head)
{
Node p;
p = (head)->next;
while(p)
{
printf("%d\n", p->data);
p = p->next;
}
}
int main(void)
{
Node head;
int i;
InitList(&head);
CreateList(&head);
printf("删除前的数据:\n");
PrintList(&head);
printf("请输入您要删除的数据:\n");
scanf("%d", &i);
DeleteList(&head, i);
printf("删除后的数据:\n");
PrintList(&head);
return 0;
}
Makefile:
#the simplest example
OBJS = tmpo
CC = gcc
CFLAGS = -Wall -O -g
tmp: $(OBJS)
$(CC) $(OBJS) -o tmp
tmpo: tmpc
$(CC) $(CFLAGS) -c tmpc -o tmpo
clean:
rm -f o ~ tmp
您好,请输入您要插入的数据:
1 2 3 4 0
删除前的数据:
1
2
3
4
请输入您要删除的数据:
1
删除后的数据:
2
3
4
#include <stdioh>
#include <stdlibh>
#include <malloch>
#include <stringh>
#define ID struct id
struct id
{
char name[20];
int num;
int a;
int b;
int c;
double ave;
ID next; //
};
int pc=0;
ID creat()
{
ID p1,p2,head;
int pd;
p1=p2=head=NULL;
printf("\t\t\t 开始输入记录(学号0结束)!\n");
while(1)
{
printf("请输入学生的学号:\n");scanf("%d",&pd);
if(pd==0) break;
p1=(ID)malloc(sizeof(ID));
p1->num=pd;
printf("请输入学生的姓名:\n");scanf("%s",p1->name);
printf("请输入学生的语文成绩:\n");scanf("%d",&p1->a);
printf("请输入学生的数学成绩:\n");scanf("%d",&p1->b);
printf("请输入学生的外语成绩:\n");scanf("%d",&p1->c);
p1->ave=(p1->a+p1->b+p1->c)/30;
if(head==NULL)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
pc++;
}
p2->next=NULL;
return(head);
}
ID sort(ID head)
{
int temp;
char str[100];
double dbl;
ID p1,p2;
for(p1=head;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if(p1->ave<p2->ave)
{
temp=p1->num;
p1->num=p2->num;
p2->num=temp;
strcpy(str,p1->name);
strcpy(p1->name,p2->name);
strcpy(p2->name,str);
temp=p1->a;
p1->a=p2->a;
p2->a=temp;
temp=p1->b;
p1->b=p2->b;
p2->b=temp;
temp=p1->c;
p1->c=p2->c;
p2->c=temp;
dbl=p1->ave;
p1->ave=p2->ave;
p2->ave=dbl;
}
}
}
printf("排序成功!!!\n");
return (head);
}
/输入/添加记录/
ID insert(ID head)
{
ID temp,p1,p2;
printf("插入 *** 作开始!!!\n");
temp=(ID )malloc(sizeof(ID));
printf("请输入学生的学号:\n");scanf("%d",&temp->num);
printf("请输入学生的姓名:\n");scanf("%s",temp->name);
printf("请输入学生的语文成绩:\n");scanf("%d",&temp->a);
printf("请输入学生的数学成绩:\n");scanf("%d",&temp->b);
printf("请输入学生的外语成绩:\n");scanf("%d",&temp->c);
temp->ave=(temp->a+temp->b+temp->c)/30;
if (head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
p1=head;
while(p1!=NULL && p1->ave > temp->ave)
{
p2=p1;
p1=p1->next;
}
p2->next=temp;
temp->next=p1;
}
printf("插入成功");
pc++;
return (head);
}
/删除学生记录/
ID delet(ID head)
{
ID p1,p2;
int num;
printf("请输入要删除的学生的学号:");scanf("%d",&num);
p1=head;
if (head==NULL)
{
printf("没有记录\n");
goto end;
}
while(num!=p1->num && p1!=NULL)
{
p2=p1;p1=p1->next;
}
if(num==p1->num)
{
if (p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("删除成功!!!\n");
pc--;
}
end:return head;
}
/查找学生记录/
ID search(ID head)
{
ID p1,p2;
char str[100];
printf("请输入要查找的学生的姓名:");scanf("%s",str);
p1=head;
while(strcmp(str,p1->name) && p1!=NULL)
{
p2=p1;p1=p1->next;
}
if(strcmp(str,p1->name)==0)
{
printf("学生的学号:%d\n",p1->num);
printf("学生的姓名:%s\n",p1->name);
printf("学生的语文成绩:%d\n",p1->a);
printf("学生的数学成绩:%d\n",p1->b);
printf("学生的外语成绩:%d\n",p1->c);
printf("学生的平均成绩:%2lf\n",p1->ave);
}
return head;
}
/显示结果函数/
void print(ID head)
{
ID p;
p=head;
printf("\t\t\t\n");
printf("显示结果是:\n");
if(head!=NULL)
do
{
printf("%10d%10s%10d%10d%10d%102lf\n",p->num,p->name,p->a,p->b,p->c,p->ave);
p=p->next;
} while(p!=NULL);
}
void main()
{
ID head=NULL;
int choise;
printf("\t\t\t C语言课设 \n");
while(1)
{
printf("\t\t 学生信息管理系统\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t 1输入\n");
printf("\t\t 2显示\n");
printf("\t\t 3查找\n");
printf("\t\t 4排序\n");
printf("\t\t 5插入\n");
printf("\t\t 6删除\n");
printf("\t\t 0退出\n");
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("请选择(0-6):");
scanf("%d",&choise);
switch(choise)
{
case 1: head=creat();
break;
case 2: print(head);
break;
case 3: head=search(head);
break;
case 4: head=sort(head);
break;
case 5: head=insert(head);
break;
case 6: head=delet(head);
break;
case 0:
exit(0);
break;
default :printf("输入错误,请重新输入!\n");
}
}
}
以上就是关于一个挺长的关于链表的C语言程序。好回答还加分!全部的内容,包括:一个挺长的关于链表的C语言程序。好回答还加分!、请教关于链表的程序、50分求用c语言编写链表程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)