把程序给你吧,纯手写,族塌坦希望有点用
#include<stdio.h>
#include<stdlib.h>
typedef
struct
sss{
int
num
struct
sss
*next
}elem
void
bigadd(elem
*sum,int
n)
void
output(elem
*a)
void
clear(elem
*a){
elem
*temp
while(a!=NULL){
temp=a
a=a->next
free(temp)
}
}
int
main()
{
int
temp,max,min
elem
*sum
elem
*recent
elem
*head
head=(elem*)calloc(1,sizeof(elem))
recent=head
max=1
min=999999999
sum=(elem*)calloc(1,sizeof(elem))
sum->num=0
sum->next=NULL
printf("Please
input
a
series
of
integers:")
scanf("%d",&head->num)
scanf("%d",&temp)
while(temp!=-1){
recent->next=(elem*)calloc(1,sizeof(elem))
recent=recent->next
recent->num=temp
if(temp>max)
max=temp
if(temp<min)
min=temp
bigadd(sum,temp)
scanf("%d",&temp)
}
recent->next=NULL
clear(head)
printf("The
maximum,minmum
and
the
total
are:%d
%d
",max,min)
output(sum)
printf("\n")
return
0
}
void
bigadd(elem
*a,int
n){
int
temp
elem
*t
a->num+=n
temp=a->num/1000000000
a->衫逗num=a->num%1000000000
if(temp!=0){
if(a->next==NULL){
a->next=(elem*)calloc(1,sizeof(elem))
t=a->next
t->next=NULL
t->num=temp
}
else
bigadd(a->next,temp)
}
}
void
output(elem
*a){
if(a->next==NULL)
printf("%d",a->num)
else{
output(a->next)
printf("%09d",a->num)
}
free(a)
}
虽然题目一个链表只要3元素,但我不想把代码写死,修改常量可实现任意长度链表。
另外你强调不能用头结点,所以我用指向首节点的指针。(头结点只是方便定位链表,和首节点指针其实意义相同,否则你去哪找之前创建好的链表)
#include <stdio.h>#include <malloc.h>
#define LN 3//最大链表元素数量,这里题目只要3个
typedef struct list
{
int num
struct list *next
}LIST
LIST * creatList()
LIST *px(LIST *listP)
void printfLIST(LIST *listP)
LIST *cLists(LIST *listP1,LIST *listP2)
int main()
{
LIST *listP1=NULL,*listP2=NULL
printf("输入:\n")
listP1=creatList()
px(listP1)
listP2=creatList()
px(listP2)
printf("输出:\n")
printfLIST(listP1)
printfLIST(listP2)
printfLIST(cLists(listP1,listP2))
return 0
}
void printfLIST(LIST *listP)//打印链表
{
while(listP)
{
printf("%d ",listP->num)
返前塌 if(listP->next==NULL)
break
listP=listP->next
}
printf("\n")
}
LIST * creatList()//输入创建单链表,返回首节点指针
{
int i=LN
LIST *listP=NULL,*listTail=NULL
while(i-->0)
{
LIST *listNew=(LIST *)malloc(sizeof(LIST))
listNew->next=NULL
scanf("%d",&listNew->num)
if(listP==NULL)
listP=listNew
else
listTail->next=listNew
listTail=listNew
}
return listP
}
LIST *px(LIST *listP)//排序,返回首节点漏圆指针
{
LIST * listf=listP,*listn=NULL
while(listf)
{
listn=listf->next
while(listn)
{
if(listf->num>listn->num)
listf->num^=listn->num,listn->num^=listf->num,listf->num^=listn->num
if(listn->next==NULL)
悔差 break
listn=listn->next
}
if(listf->next==NULL)
break
listf=listf->next
}
return listP
}
LIST *cLists(LIST *listP1,LIST *listP2)//连接两个链表,返回首节点指针
{
LIST *listP3=listP1
while(listP1)
{
if(listP1->next==NULL)
break
listP1=listP1->next
}
listP1->next=listP2
return listP3
}
#include <stdio.h>#include <string.h>
#include <stdlib.h>
#define SN 2 //科目数量(score number)
typedef struct student
{
char num[10],
name[10]
float score[SN],
sum,
avg
struct student *next
}STU
/**********输入链表单元内容************/
void input(STU *p)
{
int i
printf("please input number:\n")
scanf("%s",p->num)
printf("please input name:\n")
scanf("%s",p->name)
printf("卜宽please input %d scores:\n",SN)
p->sum=0
for(i=0i<SNi++)
{
scanf("%f",&p->score[i])
p->sum+=p->score[i]
}
p->avg=p->sum/SN
}
/**********创建一个链表单元**********/
STU *creat_node()
{
STU *p
p=(STU *)malloc(sizeof(STU))
if(p == NULL)
{ printf("No enough memory !")
exit(0)
}
input(p)
p->next=NULL
return p
}
/**********创建一个链表**********/
STU *creat_list()
{
STU *head=NULL,*p
char str[4]
printf("List creating...\n")
do
{
printf("Do you want to continue (yes/no) :")
scanf("%s",str)
if(strcmp(str,"yes")==0)
{
p=creat_node()
if(head==NULL)
p->next=head//前插法
head=p
}
if(strcmp(str,"yes")!=0&&strcmp(str,"no")!=0)
{
printf("You must input 'yes' or 'no'.\型返亮n")
//getchar()
continue
}
if(strcmp(str,"no")==0)break
//getchar()
}while(1)
printf("List create end...\n\n")
return head
}
/************输出一个链表头部**********/
void print_a_head()
{
int i
printf("number\tname\tavg\tsum\t")
for(i=0i<世唯SNi++)
printf("score%d\t",i+1)
putchar(10)
}
/************输出一个链表单元**********/
void print_a_node(STU *fin)
{
int i
printf("%s\t%s\t%0.2f\t%0.2f\t",fin->num,fin->name,fin->avg,fin->sum)
for(i=0i<SNi++)
printf("%0.2f\t",fin->score[i])
putchar(10)
}
/************输出链表**********/
int print_list(STU *stu)
{
STU *p=stu
if(stu==NULL)
{
printf("no records!!!\n")
return (0)
}
print_a_head()
while(p!=NULL)
{
print_a_node(p)
p=p->next
}
putchar(10)
return (0)
}
void main()
{
STU *head
head=creat_list()//创建链表
print_list(head)//输出链表
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)