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

顺序链表笔记

时间:2015-04-15 18:27:47      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

数据结构基础  最简单的部分  顺序链表实际就是个结构体数组  代码如下 比较简单 

  1 #include "stdio.h"
  2 #include "string.h"
  3 #define MAXSIZE 100
  4 
  5 
  6 
  7 typedef struct
  8 {
  9     char key[15];
 10     char name[20];
 11     int age;
 12 }DATA;
 13 
 14 
 15 typedef struct 
 16 {
 17     DATA ListData[MAXSIZE+1];  
 18     int ListLen;
 19 }SeqListType;
 20 
 21 
 22 
 23 void SeqListInit(SeqListType *SL);      //初始化顺序表
 24 int SeqListLength(SeqListType *SL);     //返回顺序表的元素数量
 25 int SeqListAdd(SeqListType *SL,DATA data);        //向顺序表中添加元素 
 26 int SeqListInsert(SeqListType *SL,int n,DATA data);  //向顺序表中插入元素
 27 int SeqListDelete(SeqListType *SL,int n) ;  //删除顺序表中的数据元素
 28 DATA *SeqListFindByNum(SeqListType *SL,int n);   //根据序号返回元素
 29 int SeqListFindByCont(SeqListType *SL,char *key); //按关键字查找
 30 int SeqListAll(SeqListType *sl);                //遍历顺序表中的内容
 31 
 32 
 33 int main()
 34 {
 35     int i;
 36     SeqListType SL;
 37     DATA data,*data1;
 38     char key [15];
 39     
 40     SeqListInit(&SL);
 41     do
 42     {
 43         printf("输入添加的节点:学好  姓名  年龄:");
 44         fflush(stdin);
 45         scanf("%s%s%d",&data.key,&data.name,&data.age);
 46         if(data.age)
 47         {
 48             if(!SeqListAdd(&SL,data)) //若添加节点失败
 49                 break;             
 50         } 
 51         else
 52             break;
 53     }while(1);
 54     printf("\n顺序表中的节点顺序为:\n");
 55     SeqListAll(&SL);
 56     
 57     fflush(stdin);
 58     printf("\n要取出的节点的序号:");
 59     scanf("%d",&i);
 60     data1=SeqListFindByNum(&SL,i);
 61     if(data1)
 62         printf("第%d个节点为:%s,%s,%d\n",i,data1->key,data1->name,data1->age);
 63     
 64     fflush(stdin);
 65     printf("\n要查找字节的关键字:");
 66     scanf("%s",key);
 67     i=SeqListFindByCont(&SL,key);
 68     data1=SeqListFindByNum(&SL,i);
 69     if(data1)
 70         printf("第%d个节点为:%s,%s,%d\n",i,data1->key,data1->name,data1->age);
 71 
 72     return 0;
 73 }
 74 
 75 
 76 void SeqListInit(SeqListType *sl)
 77 {
 78     sl->ListLen=0;    //初始化时,设置顺序表长度为0 
 79 } 
 80 
 81 int SeqListLength(SeqListType *SL)
 82 {
 83     return (SL->ListLen);
 84 } 
 85 
 86 int SeqListAdd(SeqListType *SL,DATA data)
 87 {
 88     if(SL->ListLen==MAXSIZE)
 89     {
 90         printf("顺序表已满,不能再添加节点了");
 91         return 0; 
 92     }
 93     SL->ListData[++SL->ListLen]= data;
 94     return 1;
 95 }
 96 
 97 int SeqListInsert(SeqListType *sl,int n,DATA data)
 98 {
 99     int i;
100     if(sl->ListLen>MAXSIZE)
101     {
102         printf("顺序表已满,不能插入节点");
103         return 0; 
104     }
105     if(n<1 || n>sl->ListLen)
106     {
107         printf("插入节点序号错误,不能插入元素");
108         return 0; 
109     }
110     for(i=sl->ListLen;i>=n;i--)
111         sl->ListData[i+1]=sl->ListData[i];
112     sl->ListData[n]=data;
113     sl->ListLen++;
114     return 1;
115 }
116 int SeqListDelete(SeqListType *sl,int n)
117 {
118     int i;
119     if(n<1 || n>sl->ListLen)
120     {
121         printf("删除节点序号错误,不能删除元素");
122         return 0; 
123     }
124     for(i=n;i<sl->ListLen;i++)
125         sl->ListData[i]=sl->ListData[i+1];
126     sl->ListLen--;
127     return 1;
128 }
129 DATA *SeqListFindByNum(SeqListType *sl,int n)
130 {
131     if(n<1 || n>sl->ListLen)
132     {
133         printf("节点序号错误,不能返回节点");
134         return 0; 
135     }
136     return &(sl->ListData[n]);
137 }
138 
139 int SeqListFindByCont(SeqListType *sl,char *key)
140 {
141     int i;
142     for(i=1;i<=sl->ListLen;i++)
143         if(strcmp(sl->ListData[i].key,key)==0)
144             return i;
145     return 0;
146 }
147 int SeqListAll(SeqListType *sl)
148 {
149     int i;
150     for(i=1;i<=sl->ListLen;i++)
151         printf("%s,%s,%d\n",sl->ListData[i].key,sl->ListData[i].name,sl->ListData[i].age);
152 }

 

顺序链表笔记

标签:

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

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