3个回答
推荐于2018-03-14
展开全部
链表是一种数据结构 是一种线形的存储结构 转一个小文章
C/C++ code 准备:动态内存分配 一、为什么用动态内存分配 但我们未学习链表的时候,如果要存储数量比较多的同类型或同结构的数据的时候,总是使用一个数组。比如说我们要存储一个班级学生的某科分数,总是定义一个float型(存在0.5分)数组: float score[30]; 但是,在使用数组的时候,总有一个问题困扰着我们:数组应该有多大? 在很多的情况下,你并不能确定要使用多大的数组,比如上例,你可能并不知道该班级的学生的人数,那么你就要把数组定义得足够大意兄弟几个当初一家子,都在一块住,这就是数组
后来都成家了,在一起住不方便,就各自分开住了,但形式上还是一家,这就是链表
C/C++ code 准备:动态内存分配 一、为什么用动态内存分配 但我们未学习链表的时候,如果要存储数量比较多的同类型或同结构的数据的时候,总是使用一个数组。比如说我们要存储一个班级学生的某科分数,总是定义一个float型(存在0.5分)数组: float score[30]; 但是,在使用数组的时候,总有一个问题困扰着我们:数组应该有多大? 在很多的情况下,你并不能确定要使用多大的数组,比如上例,你可能并不知道该班级的学生的人数,那么你就要把数组定义得足够大意兄弟几个当初一家子,都在一块住,这就是数组
后来都成家了,在一起住不方便,就各自分开住了,但形式上还是一家,这就是链表
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-19
展开全部
链表是数据结构中的一个重要类型,如果你刚学习数据结构,因该知道顺序表的储存的优点是:它可以从中直接找到你要操作的数据,缺点是:顺序表只能储存在一段连续的内存空间里。而采用链表的存储就大大提高了内存的利用率。你如果数据结构刚入门,我编写一个简单的C顺序结构供你参考:#define maxsize 100
#include "conio.h"
#include "string.h"typedef struct
{long num;<br> char name[10];<br> int age;<br> float score;<br> }record;
record st[maxsize]={{403002,"wang",18,435.5},{403003,"zhang",19,511.5}, {403001,"hung",18,465.5},{403005,"zhao",17,465.5},{403004,"jiang",18,505.5}}; int len=5;
/*---------------------------------------------------------*/ void output(int n)
{int i;<br> clrscr();<br> printf("\n num name age score \n");<br> printf("\n---------------------------------------- \n");<br> for(i=0;i<n;i++)<br> printf("\n%8ld%10s%9d%11.2f\n",st[i].num,st[i].name,st[i].age,st[i].score);<br> gotoxy(40,24);<br> getch();<br> }/*---------------------------------------------------------*/
void find()
{record x;<br> char name[10];<br> int i,n;<br> while(1)<br> {<br> clrscr();<br> printf("\n\n\n ----find----\n");<br> printf("\n 1. Number \n");<br> printf("\n 2. name \n");<br> printf("\n 0. Return \n");<br> printf("\n \n");<br> printf("\n\n\n Please select :");</p><p> scanf("%d",&n);<br> while(n<0||n>2) scanf("%d",&n);<br> if(n==0) return;<br> if(n==1)<br> {clrscr();<br> gotoxy(5,8);<br> printf("Please input a number for scearch:");<br> scanf("%ld",&x.num);<br> for(i=0;i<len;i++)<br> if(x.num==st[i].num) break;</p><p> }
else
{clrscr();<br> gotoxy(5,8);<br> printf("Please input a name for scearch:");<br> scanf("%s",name);<br> for(i=0;i<len;i++)<br> if(!(strcmp(st[i].name,name))) break;<br> } if(i<len)
{clrscr(); gotoxy(10,5);<br> printf("Found ! The man\'s data is :\n\n");<br> printf("\n num name age score \n");<br> printf("\n---------------------------------------- \n");<br> printf("\n%8ld%10s%9d%11.2f\n",st[i].num,st[i].name,st[i].age,st[i].score);<br> }
else {gotoxy(20,15);printf(" Not found !");}
gotoxy(30,20);
printf(" Press Any key to continue !\n");
getch(); } }
/*---------------------------------------------------------*/
void delete()
{ record x;
char name[10];
int i,n;
while(1)
{
clrscr();
printf("\n\n\n ----delete----\n");
printf("\n 1. Number \n");
printf("\n 2. name \n");
printf("\n 0. Return \n");
printf("\n \n");
printf("\n\n\n Please select :"); scanf("%d",&n);
while(n<0||n>2) scanf("%d",&n);
if(n==0) return;
if(n==1)
{clrscr();<br> gotoxy(5,8);<br> printf("Please input a number for scearch:");<br> scanf("%ld",&x.num);<br> for(i=0;i<len;i++)<br> if(x.num==st[i].num) break;</p><p> }
else
{clrscr();<br> gotoxy(5,8);<br> printf("Please input a name for scearch:");<br> scanf("%s",name);<br> for(i=0;i<len;i++)<br> if(!(strcmp(st[i].name,name))) break;<br> }
if(i<len)
{
for(;i+1<len;i++)
st[i]=st[i+1];
len--;
}
else printf("\nNot found !");
output(len); }
}
/*---------------------------------------------------------*/
void insert()
{record s;<br> float x;<br> char ch;<br> int n;<br> if(len+1>=maxsize)<br> {printf(" Table is full !\n");<br> printf(" Can\'t insert ! Sorry !!\n");<br> }
else
{
printf("\n\nInsert a record ! Are you sure (y/n)?");
ch=getch();
while(ch=='y'||ch=='Y')
{
printf("\n\n position:");scanf("%d",&n);
if(n>len+1||n<1) printf("Position is error !\n");
else
{int j;<br> clrscr(); printf("\n\n\n\n\n");<br> printf("Please input NO. :") ;scanf("%ld",&s.num);<br> printf("\n name :");scanf("%s", s.name);<br> printf("\n age :");scanf("%d", &s.age);<br> printf("\n score:");scanf("%f",&x);<br> s.score=x;</p><p> /* INSERT BEGIN */<br> for(j=len-1;j>=n-1;j--)<br> st[j+1]=st[j];<br> st[j+1]=s;<br> len++;<br> }
printf("\n\nContinue ?(y/n)");
ch=getch();
}
output(len);
} }/*---------------------------------------------------------*/
void append()
{float x;<br> char ch;<br> if(len+1>=maxsize)<br> {printf(" Table is full !\n");<br> printf(" Can\'t append ! Sorry !!\n");<br> }
else
{
printf("\n\nAppend ! Are you sure (y/n)?");
ch=getch();
while(ch=='y'||ch=='Y')
{clrscr(); printf("\n\n\n\n\n");<br> printf("Please input NO. :") ;scanf("%ld",&st[len].num);<br> printf("\n name :");scanf("%s", st[len].name);<br> printf("\n age :");scanf("%d", &st[len].age);<br> printf("\n score:");scanf("%f",&x);<br> st[len].score=x;<br> len++;<br> printf("\n\nContinue ?(y/n)");<br> ch=getch();<br> }
output(len);
}
}/*---------------------------------------------------------*/
void quit()
{clrscr();<br> gotoxy(20,10);<br> printf("Nice to meet you ! Welcome use again !");<br> getch();<br> exit();<br> }
/*---------------------------------------------------------*/
main()
{int n;<br> for(;;)<br> {<br> clrscr();<br> printf("\n\n\n ----menu----\n");<br> printf("\n ******************************");<br> printf("\n 1.find 2.insert \n");<br> printf("\n 3.delete 4.append \n");<br> printf("\n 5.output 0.quit \n");<br> printf("\n\n\n Please select :");</p><p> scanf("%d",&n);<br> while(n<0||n>5)<br> {printf("\n Input error!");<br> printf("\n\nPlease input again !");<br> scanf("%d",&n);<br> }
switch(n)
{case 1:find();break;<br> case 2:insert();break;<br> case 3:delete();break;<br> case 4:append();break;<br> case 5:output(len);break;<br> case 0:quit();<br> }
} }以上代码 是可以类似于 Access数据库的 简单代码,如果你希望可以对数据结构方面知识有更好的 了解可以加我Q863469857
#include "conio.h"
#include "string.h"typedef struct
{long num;<br> char name[10];<br> int age;<br> float score;<br> }record;
record st[maxsize]={{403002,"wang",18,435.5},{403003,"zhang",19,511.5}, {403001,"hung",18,465.5},{403005,"zhao",17,465.5},{403004,"jiang",18,505.5}}; int len=5;
/*---------------------------------------------------------*/ void output(int n)
{int i;<br> clrscr();<br> printf("\n num name age score \n");<br> printf("\n---------------------------------------- \n");<br> for(i=0;i<n;i++)<br> printf("\n%8ld%10s%9d%11.2f\n",st[i].num,st[i].name,st[i].age,st[i].score);<br> gotoxy(40,24);<br> getch();<br> }/*---------------------------------------------------------*/
void find()
{record x;<br> char name[10];<br> int i,n;<br> while(1)<br> {<br> clrscr();<br> printf("\n\n\n ----find----\n");<br> printf("\n 1. Number \n");<br> printf("\n 2. name \n");<br> printf("\n 0. Return \n");<br> printf("\n \n");<br> printf("\n\n\n Please select :");</p><p> scanf("%d",&n);<br> while(n<0||n>2) scanf("%d",&n);<br> if(n==0) return;<br> if(n==1)<br> {clrscr();<br> gotoxy(5,8);<br> printf("Please input a number for scearch:");<br> scanf("%ld",&x.num);<br> for(i=0;i<len;i++)<br> if(x.num==st[i].num) break;</p><p> }
else
{clrscr();<br> gotoxy(5,8);<br> printf("Please input a name for scearch:");<br> scanf("%s",name);<br> for(i=0;i<len;i++)<br> if(!(strcmp(st[i].name,name))) break;<br> } if(i<len)
{clrscr(); gotoxy(10,5);<br> printf("Found ! The man\'s data is :\n\n");<br> printf("\n num name age score \n");<br> printf("\n---------------------------------------- \n");<br> printf("\n%8ld%10s%9d%11.2f\n",st[i].num,st[i].name,st[i].age,st[i].score);<br> }
else {gotoxy(20,15);printf(" Not found !");}
gotoxy(30,20);
printf(" Press Any key to continue !\n");
getch(); } }
/*---------------------------------------------------------*/
void delete()
{ record x;
char name[10];
int i,n;
while(1)
{
clrscr();
printf("\n\n\n ----delete----\n");
printf("\n 1. Number \n");
printf("\n 2. name \n");
printf("\n 0. Return \n");
printf("\n \n");
printf("\n\n\n Please select :"); scanf("%d",&n);
while(n<0||n>2) scanf("%d",&n);
if(n==0) return;
if(n==1)
{clrscr();<br> gotoxy(5,8);<br> printf("Please input a number for scearch:");<br> scanf("%ld",&x.num);<br> for(i=0;i<len;i++)<br> if(x.num==st[i].num) break;</p><p> }
else
{clrscr();<br> gotoxy(5,8);<br> printf("Please input a name for scearch:");<br> scanf("%s",name);<br> for(i=0;i<len;i++)<br> if(!(strcmp(st[i].name,name))) break;<br> }
if(i<len)
{
for(;i+1<len;i++)
st[i]=st[i+1];
len--;
}
else printf("\nNot found !");
output(len); }
}
/*---------------------------------------------------------*/
void insert()
{record s;<br> float x;<br> char ch;<br> int n;<br> if(len+1>=maxsize)<br> {printf(" Table is full !\n");<br> printf(" Can\'t insert ! Sorry !!\n");<br> }
else
{
printf("\n\nInsert a record ! Are you sure (y/n)?");
ch=getch();
while(ch=='y'||ch=='Y')
{
printf("\n\n position:");scanf("%d",&n);
if(n>len+1||n<1) printf("Position is error !\n");
else
{int j;<br> clrscr(); printf("\n\n\n\n\n");<br> printf("Please input NO. :") ;scanf("%ld",&s.num);<br> printf("\n name :");scanf("%s", s.name);<br> printf("\n age :");scanf("%d", &s.age);<br> printf("\n score:");scanf("%f",&x);<br> s.score=x;</p><p> /* INSERT BEGIN */<br> for(j=len-1;j>=n-1;j--)<br> st[j+1]=st[j];<br> st[j+1]=s;<br> len++;<br> }
printf("\n\nContinue ?(y/n)");
ch=getch();
}
output(len);
} }/*---------------------------------------------------------*/
void append()
{float x;<br> char ch;<br> if(len+1>=maxsize)<br> {printf(" Table is full !\n");<br> printf(" Can\'t append ! Sorry !!\n");<br> }
else
{
printf("\n\nAppend ! Are you sure (y/n)?");
ch=getch();
while(ch=='y'||ch=='Y')
{clrscr(); printf("\n\n\n\n\n");<br> printf("Please input NO. :") ;scanf("%ld",&st[len].num);<br> printf("\n name :");scanf("%s", st[len].name);<br> printf("\n age :");scanf("%d", &st[len].age);<br> printf("\n score:");scanf("%f",&x);<br> st[len].score=x;<br> len++;<br> printf("\n\nContinue ?(y/n)");<br> ch=getch();<br> }
output(len);
}
}/*---------------------------------------------------------*/
void quit()
{clrscr();<br> gotoxy(20,10);<br> printf("Nice to meet you ! Welcome use again !");<br> getch();<br> exit();<br> }
/*---------------------------------------------------------*/
main()
{int n;<br> for(;;)<br> {<br> clrscr();<br> printf("\n\n\n ----menu----\n");<br> printf("\n ******************************");<br> printf("\n 1.find 2.insert \n");<br> printf("\n 3.delete 4.append \n");<br> printf("\n 5.output 0.quit \n");<br> printf("\n\n\n Please select :");</p><p> scanf("%d",&n);<br> while(n<0||n>5)<br> {printf("\n Input error!");<br> printf("\n\nPlease input again !");<br> scanf("%d",&n);<br> }
switch(n)
{case 1:find();break;<br> case 2:insert();break;<br> case 3:delete();break;<br> case 4:append();break;<br> case 5:output(len);break;<br> case 0:quit();<br> }
} }以上代码 是可以类似于 Access数据库的 简单代码,如果你希望可以对数据结构方面知识有更好的 了解可以加我Q863469857
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询