提一个关于实现C语言单链表的问题

作者&投稿:智孙 (若有异议请与网页底部的电邮联系)
C语言一个单链表问题~

print(head);


P要大写,Print(head)

struct stud-node *create(){ struct stud-node *head=NULL,*tail=NULL,*p; int num; char name[20]; int score; scanf("%d%s%d",&num,name,&score);//输入第一个学生信息 if(num==0)//学号为0表示无任何信息,直接返回空 return NULL; while(num!=0)//当学号均为有效学号时(即不为0) { p=(struct stud-node *)malloc(sizeof(struct stud-node));//为待插入节点分配内存 p->num=num,strcpy(p->name,name),p->score=score;//将学号、姓名、分数存放至待插入节点 p->next=NULL;//将待插入节点的下一个节点指向空(即0x00000000) if(head==NULL)//如果当前链表为空,则将待插入节点赋给表头和表尾 head=tail=p; else//若当前链表已有节点存在 { tail->next=p;//将待插入节点插入表尾(即tail->next) tail=p;//表尾指针向后移动(因为新插入了一个节点,此时tail所指向的节点是倒数第二个节点) } scanf("%d%s%d",&num,name,&score); } return(head); }NULL是定义在stdio.h的一个宏,即#define NULL 0

#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
char sex[5];
long age;
long score;
struct student*next;
};
int n;
struct student*creat(void)
{
struct student*head;
struct student*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score);
}
p2->next=NULL;
return(head);
}

void print(struct student *head)
{
struct student *p;
printf("\nNow,these %d records are(结果数是%d,结果是):\n",n,n);
p=head;
if(head!=NULL)
do
{
printf("%ld %s %s %ld %ld\n",p->num,p->name,p->sex,p->age,p->score);
p=p->next;
}
while(p!=NULL);
}

void paixu(struct student *head)
{
struct student *p,*q,*small;
int temp;
for(p=head;p->next!=NULL;p=p->next)
{
small=p;
for(q=p;q;q=q->next)
if(q->num<small->num)
small=q;
if(small!=p)
{
temp=p->num;
p->num=small->num;
small->num=temp;
}
}
print(head);
}

struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}

struct student *search(struct student *head,long num)
{
struct student *p;
if(head==NULL)
{
printf("\nlist null! \n");
}
p=head;
while((num!=p->num)&&(p->next!=NULL))
{
p=p->next;
}
if(num==p->num)
printf("%ld %s %s %ld %ld",p->num,p->name,p->sex,p->age,p->score);
else
printf("num not been found(找不到)!\n");
return(head);
}

struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null(无数据)! \n");
}
p1=head;
while((num!=p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete(删除):%ld\n",num);
n=n-1;
}
else
printf("num not been found(无效的数字)!\n");
return(head);
}

main()
{
struct student *head,*stu;
long del_num;
long search_num;
int x;
do
{
printf("***************\n1 create new chain(制作新链表)\n2 insert data(插入数据)\n3 query data(查询数据)\n4 output data(打印数据)\n5 delete data(删除数据)\n0 end(退出)\n***************\n");
printf("Please input number 0-5 to test the program(请输入0-5选择测试):\n");

scanf("%d",&x);
if(x==1)
{
printf("input records(Ex: num name sex age score)(输入数据如:学号 姓名 性别 年龄 分数):\n");
head=creat();
}
if(x==4)
{
paixu(head);
}
if(x==2)
{
printf("\ninput the inserted record(插入新数据):");
stu=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record(插入新数据):");
stu=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score);
}
}
if(x==3)
{
printf("\ninput the searched number(输入查询学号):");
scanf("%ld",&search_num);
while(search_num!=0)
{
head=search(head,search_num);
printf("\ninput the searched number(输入查询学号):");
scanf("%ld",&search_num);
}
}
if(x==5)
{
printf("\ninput the deleted number(输入删除学号):");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("input the deleted number(输入删除学号):");
scanf("%ld",&del_num);
}
}
}while(x!=0);
}

#include<stdio.h>
#include<malloc.h>
typedef struct student{
long lNumber;
char cName[20];
char iSex;
int iAge;
struct student *next;
}student;

void main()
{
student *head,*p,*q;
long num;

int n=0;
p=(student *)malloc(sizeof(student));
scanf("%ld%s%c%d",&p->lNumber,&p->cName,&p->iSex,&p->iAge);
q=p;
while ( p!=0 )
{
n++;
if (n==1)
{
head=p;
}
q=p;
p=p->next;
p=(student*)malloc(sizeof(student));
printf("please input the %d student's information:\n");
scanf("%ld%s%c%d",&p->lNumber,&p->cName,&p->iSex,&p->iAge);
}//输入学生的信息
p->next = NULL;
printf("now seek the student\n");//以下是用来查找该学生的
p=head->next;
n=1;
scanf("%ld",&num);
while (p!=NULL)
{
n++;
if (num==p->lNumber)
printf("the serial is :%d",n);
p=p->next;
}
}

"性别" 输入时,你用F代表"男",M代表"女"吧
如果用的编译器是vc++6.0的话,程序还可以改得更加完善一些,不会向这个这样烂,其实这道题,把它写成由几个子函数构成的程序效果会更好.你不妨试试.

或者

压仓底的东西再次拿出,有点不同

/*系统功能是:实现班级学生信息管理,学生信息字段:(不能相同),姓名,籍贯,出生年月四个字段。 程序能对
学生信息进行输入、查询、删除、保存、依照学号排序等操作。*/
#include<stdio.h>
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 10
//-------------------------------------------------------------------------------
//函数种类
/* void dakai(int n,int j,char ch);
void save(int n,char ch);
void display(int n,char ch);//显示信息
void xiugai();
void dakai(int n,int j,char ch);//查找文件
void shuru(int n,char ch);//输入信息
void paixu(int n,char ch);
void xuanzhe(int n,char ch);//选择运行程序*/

//-------------------------------------------------------------------------------
struct student_type//定义结构体
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];

//-----------------------------------------------------------------------------------------

void save(int n,char ch)//保存输入信息
{

FILE *fp;
int i;
if((fp=fopen("f:\\stu_list.txt","wb+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<n;i++)
if(fwrite(&stud[i],sizeof(student_type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
//------------------------------------------------------------------------------------------

void display(int n,char ch)//显示信息
{
FILE *fp;
int i;
if((fp=fopen("f:\\stu_list.txt","rb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<n;i++)
{
fread(&stud[i],sizeof(struct student_type),1,fp);
printf("姓名:%-10s学号:%4d年龄:%4d地址:%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
}
//--------------------------------------------------------------------------------------------

void xiugai(int n,char ch)//修改学生信息
{
int z;
FILE *pf;
pf=fopen("f:\\stu_list.txt","rb+");
if(pf==NULL)
{
printf("\n pf open Error,\n exit program.");
return;
}
else
{
//输入学生信息
cout<<"请输入你要修改的学号:"<<endl;
cin>>z;
fseek(pf,z*sizeof(struct student_type),2);
scanf("%s%d%d%s",stud[z].name,&stud[z].num,&stud[z].age,stud[z].addr);
fseek(pf,sizeof(struct student_type),0);
fwrite(&stud,sizeof(struct student_type),1,pf);
}
fclose(pf);
//save(n,ch);
//display(n,ch);

}

//-----------------------------------------------------------------------------------------------
/* struct stud
{
int num;
struct studt;
} *head;

void printnode(stud
{
while(p->next!=NULL)
{
//输出p指向的数据
cout<<p->num<<" ";
p=p->next;//指向下一个
}
cout<<p->num<<endl;
}

//---------------------------------------------------------------------------------------------

void dakai2()
{
node *p=new node;
head=p;//设置头指针
p->num=90;

for(int k=1;k<10;k++)
{
//产生下一个结点1
p->next=p+k;
p=p->next;
p->num=k*100;
}
p->next=NULL;
printnode(head);
}*/

//------------------------------------------------------------------------------------------------

void dakai(int n,int j,char ch)//查找文件
{
//int i=2;
//char ch1,filename[20];
FILE *fp;
//int i=0;
//display(n,ch);
//cout<<"请输入所要查找的文件名,以'#'结束输入"<<endl;
//scanf("%s",filename);
cout<<"---------------------------------------------------------------------------"<<endl;
if ((fp=fopen("f:\\stu_list.txt","rb"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
for(int i=0;i<4;i+=1)
{
fseek(fp,i*sizeof(struct student_type),2);
fread(&stud[i],sizeof(struct student_type),1,fp);
if(stud[i].num==j)
//{
printf("姓名:%-10s学号:%4d年龄:%4d地址:%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);

}
//i++;
//}

// }
/*fread(&stud[i-1],sizeof(struct student_type),i,fp);
cout<<"姓名";
printf("%-10s",stud[i-1].name);
cout<<"学号";
printf("%4d",stud[i-1].num);
cout<<"年龄";
printf("%4d",stud[i-1].age);
cout<<"地址";
printf("%-15s\n",stud[i-1].addr);
// }
/*
while((ch=getchar())!='#')
{
fputc(ch,fp);
putchar(ch);
}*/
fclose(fp);
}

//----------------------------------------------------------------------------------------------------------

void shuru(int n,char ch)//输入信息
{
int i;
cout<<"现在请输入学生个数:";
cin>>n;
cout<<"现在请输入"<<n<<"个学生的信息:"<<endl;
for(i=0;i<n;i++)
{

cout<<"请输入第"<<i+1<<"个学生的姓名,学号,年龄,地址:"<<endl;
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);

}
save(n,ch);
display(n,ch);

}

//----------------------------------------------------------------------------------------------------------

void paixu(int n,char ch)
{
fstream outfile,infile;
infile.open("f:\\stu_list.txt",ios::in);
if(!infile)
{
cerr<<"文件打开失败!"<<endl;
exit(0);
}
student_type textline[80];
// int i=0;
// while(!infile.eof())
// {
// i++;
// infile.getline(textline,sizeof(textline));
// if
//cout<<i<<":"<<textline<<endl;
}
infile.close();
}
//-------------------------------------------------------------------------------------------------------
/* void paixu2()
{
student_type ww;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{

if(stud[i].num>stu[j].num)
{
ww.num=stud[i].num;
stud[i].num=stud[j].num;
stud[j].num=ww.num;
ww.
*/

//-----------------------------------------------------------------------------------------------------------

void xuanzhe(int n,char ch)//选择运行程序
{
int i,j;
if(ch=='s')//输入
{
shuru(n,ch);
cout<<"---------------------------------------------------------------------"<<endl;
cout<<" 请输入命令"<<endl<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}
else if(ch=='f')//查找
{
cout<<"请输入要查找的编号:"<<endl;
cin>>j;
dakai(n,j,ch);
cout<<"---------------------------------------------------------------------"<<endl;
cout<<" 请输入命令"<<endl<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}
else if(ch=='a')//保存
{
save(n,ch);
cout<<"---------------------------------------------------------------------"<<endl;
cout<<" 请输入命令"<<endl<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}
else if(ch=='p')
{
paixu(n,ch);
cout<<"---------------------------------------------------------------------"<<endl;
cout<<" 请输入命令"<<endl<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}
else if(ch=='x')//修改
{
xiugai(n,ch);
cout<<"------------------------------------------------------------------------"<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}
else if(ch=='d')
{
display(n,ch);//显示信息
cout<<"------------------------------------------------------------------------"<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}

else
{
cout<<"命令错误!"<<endl;
}
}

//--------------------------------------------------------------------------------------------------

void main()//主函数
{
int n;
char ch;
cout<<"---------------------------------------------------------------------"<<endl;
cout<<" 请输入命令"<<endl;
cout<<"s(输入信息)、f(查找信息)、d(删除)、p(排序)、a(存储信息)按其他键退出"<<endl;
cin>>ch;
xuanzhe(n,ch);
}

作业 自己看着办 我是不会帮你做的 但是可以跟你一样本 以前刚学的时候写的 很白痴的 大部分有 没排列

嘿嘿,我有点看不明白哟!帮不上你了!@_@

是不是刚上大一啊。。。当初我也不会,现在想想没什么!好好努力吧。。。

我不会搞,不然就给你做个

一个c语言的程序题?
答:然后,在main()函数中定义一个头节点指针,并用它来存储整个链表 请点击输入图片描述 接下来,实现从键盘输入五个整数并将它们尾插入链表中 请点击输入图片描述 最后,实现链表中的插入和删除操作,并输出链表中的所有元素 下面是一个可行的c语言程序,该程序实现了从键盘输入五个整数并存储在链表中,...

编写一个C语言程序 实现单链表的基本操作
答:printf("链表为空\n");else printf("链表不空\n");len = length_list(pHead);printf("链表的长度为: %d\n", len);sort_list(pHead);traverse_list(pHead);printf("请输入您要在第几个节点插入\n");scanf("%d", &i);printf("请输入您要在第%d个节点插入的值\n", i);scanf("%d"...

关于C语言单向链表,编写一个主函数,要求用函数实现如下功能:
答:int count = 0; while (current_node->next != NULL && count < index - 1) { current_node = current_node->next; count++; } if (count == index - 1) { node->next = current_node->next; current_node->next = node; } } void output() { ...

谁能给我个C语言最简单的单链表例子。只要有创建和输出就足够了 要动...
答:= Date;p->pstnext = s;p = s;} else { cycle = 0;} } p->pstnext = NULL;return(head);} int main(){ Node *Head = NULL; //定义头结点 Node *Head_New = NULL;//链表建立 Head = creat();printf("输出建立的单链表\r\n");//链表输出 output(Head);return 0;} ...

C语言里面关于链表的一个函数
答:单链表的插入呀 for的意思是先创建一个空节点,p指向它,然后你输入一个元素,把这个元素放到这个节点的info域,即scanf(“%c”,&p->info),这句应该在printf后面,你掉了 然后把这个p赋值给q的next域,应该是q->next=p;你写错了,即头节点指向下一个节点了 之后q=p;是让q指向这个新节点...

帮我用C语言写一个数据结构中单链表的建立和插入以及删除操作,就只写这...
答:include<stdio.h> include<malloc.h> typedef struct Node //定义节点 { int data; //可以为其他类型 struct Node *next;}List;void create(struct Node *&L,int a[],int n) //创建,L是链表头,a是要插入数组,n是要插入元素个数 { List *s;int i;L=(List *)malloc(sizeof(Li...

用c语言编写,实现单链表的建立(数据元素为1,2,3,4,5,6),遍历,查找,插 ...
答:typedef struct List { int data;List* next;}List;List* Init(int n){ List *p, *q, *head;int i = 0;q = (List*)malloc(sizeof(List));p = (List*)malloc(sizeof(List));head = (List*)malloc(sizeof(List));head = p;while (n != i){ i++;printf("第%d个元素:", ...

有序单链表的实现 用c语言编程
答:for(j = L->size; j > i; j--) L->list[j] = L->list[j-1]; /*为插入做准备*/ L->list[i] = x; /*插入*/ L->size ++; /*元素个数加1*/ return 1;} int ListDelete(SeqList *L, DataType x)/*删除顺序表L中的数据元素值x*/ /*删除成功返回1,删除失败...

求C语言建立单链表的算法
答:include<stdio.h> struct stu { char name[10];struct stu *next;} main(){ struct stu *head;head=(struct stu*)malloc(sizeof(struct stu));strcpy(head->name,"apple");head->next=NULL;} 这就是最简单链表的建立,这个链表里面只有一个链 顺便说下,C+就是C++ ...

帮我编写一个用C语言编写的单链表的建立,和输入输出操作,谢谢各位_百度...
答:include <stdio.h> include <windows.h> typedef struct node { int num;struct node *next;}lnode;lnode *creat(){ lnode *head,*p,*q;int n;head=NULL;printf("输入要创建的节点数\n");scanf("%d",&n);while(n){ p=(lnode *)malloc(sizeof(lnode));printf("输入数据\n");sc...