码迷,mamicode.com
首页 > 其他好文 > 详细

顺序链表笔记

时间:2015-04-15 13:23:57      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdio.h"
#include "string.h"
#define MAXSIZE 100

 

typedef struct
{
char key[15];
char name[20];
int age;
}DATA;


typedef struct
{
DATA ListData[MAXSIZE+1];
int ListLen;
}SeqListType;

 

void SeqListInit(SeqListType *SL); //³õʼ»¯Ë³Ðò±í
int SeqListLength(SeqListType *SL); //·µ»Ø˳Ðò±íµÄÔªËØÊýÁ¿
int SeqListAdd(SeqListType *SL,DATA data); //Ïò˳Ðò±íÖÐÌí¼ÓÔªËØ
int SeqListInsert(SeqListType *SL,int n,DATA data); //Ïò˳Ðò±íÖвåÈëÔªËØ
int SeqListDelete(SeqListType *SL,int n) ; //ɾ³ý˳Ðò±íÖеÄÊý¾ÝÔªËØ
DATA *SeqListFindByNum(SeqListType *SL,int n); //¸ù¾ÝÐòºÅ·µ»ØÔªËØ
int SeqListFindByCont(SeqListType *SL,char *key); //°´¹Ø¼ü×Ö²éÕÒ
int SeqListAll(SeqListType *sl); //±éÀú˳Ðò±íÖеÄÄÚÈÝ


int main()
{
int i;
SeqListType SL;
DATA data,*data1;
char key [15];

SeqListInit(&SL);
do
{
printf("ÊäÈëÌí¼ÓµÄ½Úµã:ѧºÃ ÐÕÃû ÄêÁ䣺");
fflush(stdin);
scanf("%s%s%d",&data.key,&data.name,&data.age);
if(data.age)
{
if(!SeqListAdd(&SL,data)) //ÈôÌí¼Ó½Úµãʧ°Ü
break;
}
else
break;
}while(1);
printf("\n˳Ðò±íÖеĽڵã˳ÐòΪ£º\n");
SeqListAll(&SL);

fflush(stdin);
printf("\nҪȡ³öµÄ½ÚµãµÄÐòºÅ:");
scanf("%d",&i);
data1=SeqListFindByNum(&SL,i);
if(data1)
printf("µÚ%d¸ö½ÚµãΪ£º%s,%s,%d\n",i,data1->key,data1->name,data1->age);

fflush(stdin);
printf("\nÒª²éÕÒ×ֽڵĹؼü×Ö:");
scanf("%s",key);
i=SeqListFindByCont(&SL,key);
data1=SeqListFindByNum(&SL,i);
if(data1)
printf("µÚ%d¸ö½ÚµãΪ£º%s,%s,%d\n",i,data1->key,data1->name,data1->age);

return 0;
}


void SeqListInit(SeqListType *sl)
{
sl->ListLen=0; //³õʼ»¯Ê±£¬ÉèÖÃ˳Ðò±í³¤¶ÈΪ0
}

int SeqListLength(SeqListType *SL)
{
return (SL->ListLen);
}

int SeqListAdd(SeqListType *SL,DATA data)
{
if(SL->ListLen==MAXSIZE)
{
printf("˳Ðò±íÒÑÂú£¬²»ÄÜÔÙÌí¼Ó½ÚµãÁË");
return 0;
}
SL->ListData[++SL->ListLen]= data;
return 1;
}

int SeqListInsert(SeqListType *sl,int n,DATA data)
{
int i;
if(sl->ListLen>MAXSIZE)
{
printf("˳Ðò±íÒÑÂú£¬²»ÄܲåÈë½Úµã");
return 0;
}
if(n<1 || n>sl->ListLen)
{
printf("²åÈë½ÚµãÐòºÅ´íÎ󣬲»ÄܲåÈëÔªËØ");
return 0;
}
for(i=sl->ListLen;i>=n;i--)
sl->ListData[i+1]=sl->ListData[i];
sl->ListData[n]=data;
sl->ListLen++;
return 1;
}
int SeqListDelete(SeqListType *sl,int n)
{
int i;
if(n<1 || n>sl->ListLen)
{
printf("ɾ³ý½ÚµãÐòºÅ´íÎ󣬲»ÄÜɾ³ýÔªËØ");
return 0;
}
for(i=n;i<sl->ListLen;i++)
sl->ListData[i]=sl->ListData[i+1];
sl->ListLen--;
return 1;
}
DATA *SeqListFindByNum(SeqListType *sl,int n)
{
if(n<1 || n>sl->ListLen)
{
printf("½ÚµãÐòºÅ´íÎ󣬲»ÄÜ·µ»Ø½Úµã");
return 0;
}
return &(sl->ListData[n]);
}

int SeqListFindByCont(SeqListType *sl,char *key)
{
int i;
for(i=1;i<=sl->ListLen;i++)
if(strcmp(sl->ListData[i].key,key)==0)
return i;
return 0;
}
int SeqListAll(SeqListType *sl)
{
int i;
for(i=1;i<=sl->ListLen;i++)
printf("%s,%s,%d\n",sl->ListData[i].key,sl->ListData[i].name,sl->ListData[i].age);
}

顺序链表笔记

标签:

原文地址:http://www.cnblogs.com/threezj/p/4428142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!