整理--数据结构--线性表

                                  顺序表:

 

//查找运算

 

int  Locate(SeqList L, ElemType e) 

 

{     int i=0;       // i为扫描计数器,初值为0,即从第一个元素开始比较

 

       while ((i<=L.last)&&(L.elem[i]!=e))

 

                    //顺序扫描表,直到找到值为key的元素, 或扫描到表尾而没找到

 

                    i++; 

 

          if  (i<=L.last)

 

                    return(i+1);  //若找到值为e的元素,则返回其序号

 

          else

 

                    return(-1);  //若没找到,则返回空序号    }

 

//插入运算

 

/*在顺序表L中第i个数据元素之前插入一个元素e。 插入前表长n=L->last+1

 

i的合法取值范围是 1≤i≤L->last+2  */

 

int  InsList(SeqList *L,int i,ElemType e)//插入运算

 

{         int k;

 

          if((i<1) || (i>L->last+2)) /*首先判断插入位置是否合法*/

 

          {         printf("插入位置i值不合法");

 

                    return(ERROR);       }

 

          if(L->last>= MAXSIZE-1)

 

          {        printf("表已满无法插入");

 

                    return(ERROR);       }

 

          for(k=L->last;k>=i-1;k--)   /*为插入元素而移动位置*/

 

                    L->elem[k+1]=L->elem[k];

 

          L->elem[i-1]=e;   /*C语言数组中,第i个元素的下标为i-1*/

 

          L->last++;

 

          return(OK);         }

 

//删除运算

 

int  DelList(SeqList *L,int i,ElemType *e) 

 

/*在顺序表L中删除第i个数据元素,并用指针参数e返回其值。i的合法取值为1≤i≤L.last+1 */    

 

{         int k;

 

          if((i<1)||(i>L->last+1))   

 

          {         printf("删除位置不合法!");

 

                    return(ERROR);       }

 

          *e = L->elem[i-1];  /* 将删除的元素存放到e所指向的变量中*/

 

          for(k=i; i<=L->last; k++)

 

                    L->elem[k-1] = L->elem[k];  /*将后面的元素依次前移*/

 

          L->last--;

 

          return(OK);         }

 

void      merge(SeqList *LA,  SeqList *LB,  SeqList *LC)

 

{         int i,j,k;

 

          i=0;j=0;k=0;

 

          while(i<=LA->last&&j<=LB->last)

 

                    if(LA->elem[i]<=LB->elem[j])

 

                    {         LC->elem[k]= LA->elem[i];

 

                               i++; 

 

                               k++;}

 

                    else

 

                    {         LC->elem[k]=LB->elem[j];

 

                               j++; 

 

                               k++;       }

 

          while(i<=LA->last)  /*当表LA有剩余元素时,则将表LA余下的元素赋给表LC*/

 

          {         LC->elem[k]= LA->elem[i];

 

                    i++; 

 

                    k++;      }

 

          while(j<=LB->last)  /*当表LB有剩余元素时,则将表LB余下的元素赋给表LC*/         

 

          {         LC->elem[k]= LB->elem[j];

 

                    j++; 

 

                    k++;      }

 

          LC->last=LA->last+LB->last+1;}

 

 

                                   单链表:

 

//对单链表进行初始化

 

void init_linklist(LinkList *l)

 

{         *l=(LinkList)malloc(sizeof(Node)); /*申请结点空间*/

 

          (*l)->next=NULL;                   /*置为空表*/    }

 

//头插法建立链表

 

void CreateFromHead(LinkList   L) 

 

{         Node   *s;

 

          char      c;

 

          int       flag=1;

 

          while(flag)   /* flag初值为1,当输入"___FCKpd___71quot;时,置flag0,建表结束*/

 

          {         c=getchar();   

 

                    if(c!='
                    {         s=(Node*)malloc(sizeof(Node)); /*建立新结点s*/

 

                               s->data=c;

 

                               s->next=L->next;/*s结点插入表头*/

 

                               L->next=s;                      }

 

                    else

 

                               flag=0;    }}

 

//尾插法建立链表

 

void CreateFromTail(LinkList L) { 

 

          Node *r, *s;

 

          char c;

 

          int   flag =1; /*设置一个标志,初值为1,当输入"___FCKpd___84quot;时,flag0,建表结束*/

 

          r=L;           /*r指针动态指向链表的当前表尾,以便于做尾插入,其初值指向头结点*/

 

          while(flag)         /*循环输入表中元素值,将建立新结点s插入表尾*/

 

          {         c=getchar();

 

                    if(c!='
                    {         s=(Node*)malloc(sizeof(Node));

 

                               s->data=c;

 

                               r->next=s;

 

                               r=s;       }

 

                    else

 

                    {flag=0;

 

                               r->next=NULL;   /*将最后一个结点的next链域置为空,表示链表的结束*/}   }   } 

 

//单链表按序号查找

 

Node * Get (LinkList  L, int i)

 

/*在带头结点的单链表L中查找第i个结点,若找到(1≤i≤n),则返回该结点的存储位置; 否则返回NULL*/

 

{         int j;

 

          Node  *p;

 

          p=L;

 

          j=0;   /*从头结点开始扫描*/ 

 

          while ((p->next!=NULL)&&(j<i))

 

          {         p=p->next;    /* 扫描下一结点*/

 

                    j++;   /* 已扫描结点计数器 */            }

 

          if(i == j)

 

                    return p;   /* 找到了第i个结点 */

 

          else 

 

                    return NULL;   /* 找不到,i≤0i>n */  }

 

//单链表按值查找

 

Node * Get (LinkList  L, int i)

 

/*在带头结点的单链表L中查找第i个结点,若找到(1≤i≤n),则返回该结点的存储位置; 否则返回NULL*/

 

{         int j;

 

          Node  *p;

 

          p=L;

 

          j=0;   /*从头结点开始扫描*/ 

 

          while ((p->next!=NULL)&&(j<i))

 

          {         p=p->next;    /* 扫描下一结点*/

 

                    j++;   /* 已扫描结点计数器 */  }

 

          if(i == j)

 

                    return p;   /* 找到了第i个结点 */

 

          else 

 

                    return NULL;   /* 找不到,i≤0i>n */            }

 

//求表长

 

int       ListLength(LinkList L)

 

/*求带头结点的单链表L的长度*/

 

{         Node *p;

 

          int j;

 

          p=L->next;

 

          j=0;   /*用来存放单链表的长度*/

 

          while(p!=NULL)

 

          {         p=p->next;

 

                    j++;      }

 

          return j; /*j为求得的单链表长度*/       }

 

//单链表的插入运算

 

int InsList(LinkList L,int i,ElemType e)

 

/*在带头结点的单链表L中第i个位置插入值为e的新结点s*/

 

{         Node *pre,*s;

 

          int k;

 

          pre=L;  

 

          k=0;                     /*""开始,查找第i-1个结点*/

 

          while(pre!=NULL&&k<i-1)  /*表未查完且未查到第i-1个时重复,找到pre指向第i-1*/ 

 

          {         pre=pre->next;

 

                    k=k+1;     }                                                                                          /*查找第i-1结点*/

 

          if(!pre)      /*如当前位置pre为空表已找完还未数到第i个,说明插入位置不合理*/ 

 

          {         printf("插入位置不合理!");

 

                    return ERROR;        }

 

          s=(Node*)malloc(sizeof(Node));   /*申请一个新的结点S */

 

          s->data=e;                       /*e置入s的数据域*/

 

          s->next=pre->next;                                 /*修改指针,完成插入操作*/

 

          pre->next=s;

 

          return OK;                     }

 

//单链表的删除运算

 

int DelList(LinkList L,int i,ElemType *e)

 

/*在带头结点的单链表L中删除第i个元素,并将删除的元素保存到变量*e*/

 

{         Node *pre,*r;

 

          int k;

 

          pre=L;

 

          k=0;

 

          while(pre->next!=NULL && k<i-1)       /*寻找被删除结点i的前驱结点i-1使p指向它*/

 

          {         pre=pre->next; 

 

                    k=k+1;    }                   

 

/*查找第i-1个结点*/

 

          if(!(pre->next))     /* while循环是因为p->next=NULLi<1而跳出的,而是因为没有找到合法的前驱位置,说明删除位置i不合法。*/

 

          {         printf("删除结点的位置i不合理!");

 

                    return ERROR;        }

 

          r=pre->next;

 

          pre->next=pre->next->next;    /*修改指针,删除结点r*/

 

          *e = r->data;

 

          free(r);    /*释放被删除的结点所占的内存空间*/

 

          printf("成功删除结点!");

 

          return OK;          }

 

//单链表有序合并

 

LinkList MergeLinkList(LinkList LA, LinkList LB)

 

/*将递增有序的单链表LALB合并成一个递增有序的单链表LC*/

 

{         Node *pa,*pb;

 

          Node *r;

 

          LinkList LC;

 

/*LC初始置空表。papb分别指向两个单链表LALB中的第一个结点,r初值为LC*/

 

          pa=LA->next;

 

          pb=LB->next;

 

          LC=LA;

 

          LC->next=NULL;

 

          r=LC;

 

/*当两个表中均未处理完时,比较选择将较小值结点插入到新表LC中。*/

 

          while(pa!=NULL && pb!=NULL)

 

          {         if(pa->data <= pb->data)

 

                    {         r->next=pa;

 

                               r=pa;

 

                               pa=pa->next;         }

 

                    else

 

                    {         r->next=pb;

 

                               r=pb;

 

                               pb=pb->next;         }}

 

          if(pa) /*若表LA未完,将表LA中后续元素链到新表LC表尾*/

 

                    r->next=pa;

 

          else       /*否则将表LB中后续元素链到新表LC表尾*/

 

                    r->next=pb;

 

          free(LB);

 

          return(LC);         }

 

//循环链表合并算法1

 

LinkList   merge_1(LinkList LA,LinkList LB)

 

{  /*此算法将两个采用头指针的循环单链表的首尾连接起来*/

 

          Node *p, *q;

 

          p=LA;

 

          q=LB;

 

          while (p->next!=LA) p=p->next;          /*找到表LA的表尾,用p指向它*/

 

          while (q->next!=LB) q=q->next;          /*找到表LB的表尾,用q指向它*/

 

          q->next=LA;         /*修改表LB 的尾指针,使之指向表LA 的头结点*/

 

          p->next=LB->next; /*修改表LA的尾指针,使之指向表LB 中的第一个结点*/

 

          free(LB);

 

          return(LA);         }

 

//循环链表合并算法2

 

LinkList  merge_2(LinkList RA,LinkList RB)

 

{  /*此算法将两个采用尾指针的循环链表首尾连接起来*/

 

          Node *p;

 

          p=RA->next; /*保存链表RA的头结点地址*/

 

          RA->next=RB->next->next;/*链表RB的开始结点链到链表RA的终端结点之后*/

 

          free(RB->next);/*释放链表RB的头结点*/

 

          RB->next=p;/*链表RA的头结点链到链表RB的终端结点之后*/

 

    return  RB;/*返回新循环链表的尾指针*/          }

 

//双向链表单插入运算

 

int DlinkIns(DoubleList L,int i,ElemType e)

 

{         DNode  *s,*p;

 

          int k;

 

          p=L;  

 

          k=0;                     /*""开始,查找第i-1个结点*/

 

          while(p->next!=L&&k<i)  /*表未查完且未查到第i-1个时重复,找到p指向第i*/ 

 

          {         p=p->next;

 

                    k=k+1; }                                       /*查找第i-1结点*/

 

          if(p->next == L)      /*如当前位置p为空表已找完还未数到第i个,说明插入位置不合*/ 

 

          {         printf("插入位置不合理!");

 

                    return ERROR;                  }

 

          s=(DNode*)malloc(sizeof(DNode));

 

          if (s)

 

          {         s->data=e;

 

                    s->prior=p->prior;             

 

                    p->prior->next=s;   

 

                    s->next=p;                               

 

                    p->prior=s;                              

 

                    return OK;          }

 

          else 

 

                    return ERROR;       }

 

//双向链表单删除运算

 

int       DlinkDel(DoubleList L,int i,ElemType *e)

 

{         DNode  *p;

 

          int k;

 

          p=L;  

 

          k=0;                     /*""开始,查找第i个结点*/

 

          while(p->next!=L && k<i)  /*表未查完且未查到第i个时重复,找到p指向第i*/ 

 

          {         p=p->next;

 

                    k=k+1;              }                                                                                 

 

          if(p->next == L)       

 

          {         return ERROR;       }

 

          else

 

          {         *e=p->data;

 

                    p->prior->next=p->next;

 

                    p->next->prior=p->prior;

 

                    free(p);

 

                    return OK;}}

 

    

)

 

___FCKpd___74
___FCKpd___75
___FCKpd___76
___FCKpd___77
___FCKpd___78
___FCKpd___79
___FCKpd___80
___FCKpd___81
___FCKpd___82
___FCKpd___83
___FCKpd___84
___FCKpd___85
___FCKpd___86
___FCKpd___87
___FCKpd___88
___FCKpd___89
___FCKpd___90
___FCKpd___91
___FCKpd___92
___FCKpd___93
___FCKpd___94
___FCKpd___95
___FCKpd___96
___FCKpd___97
___FCKpd___98
___FCKpd___99
___FCKpd___100
___FCKpd___101
___FCKpd___102
___FCKpd___103
___FCKpd___104
___FCKpd___105
___FCKpd___106
___FCKpd___107
___FCKpd___108
___FCKpd___109
___FCKpd___110
___FCKpd___111
___FCKpd___112
___FCKpd___113
___FCKpd___114
___FCKpd___115
___FCKpd___116
___FCKpd___117
___FCKpd___118
___FCKpd___119
___FCKpd___120
___FCKpd___121
___FCKpd___122
___FCKpd___123
___FCKpd___124
___FCKpd___125
___FCKpd___126
___FCKpd___127
___FCKpd___128
___FCKpd___129
___FCKpd___130
___FCKpd___131
___FCKpd___132
___FCKpd___133
___FCKpd___134
___FCKpd___135
___FCKpd___136
___FCKpd___137
___FCKpd___138
___FCKpd___139
___FCKpd___140
___FCKpd___141
___FCKpd___142
___FCKpd___143
___FCKpd___144
___FCKpd___145
___FCKpd___146
___FCKpd___147
___FCKpd___148
___FCKpd___149
___FCKpd___150
___FCKpd___151
___FCKpd___152
___FCKpd___153
___FCKpd___154
___FCKpd___155
___FCKpd___156
___FCKpd___157
___FCKpd___158
___FCKpd___159
___FCKpd___160
___FCKpd___161
___FCKpd___162
___FCKpd___163
___FCKpd___164
___FCKpd___165
___FCKpd___166
___FCKpd___167
___FCKpd___168
___FCKpd___169
___FCKpd___170
___FCKpd___171
___FCKpd___172
___FCKpd___173
___FCKpd___174
___FCKpd___175
___FCKpd___176
___FCKpd___177
___FCKpd___178
___FCKpd___179
___FCKpd___180
___FCKpd___181
___FCKpd___182
___FCKpd___183
___FCKpd___184
___FCKpd___185
___FCKpd___186
___FCKpd___187
___FCKpd___188
___FCKpd___189
___FCKpd___190
___FCKpd___191
___FCKpd___192
___FCKpd___193
___FCKpd___194
___FCKpd___195
___FCKpd___196
___FCKpd___197
___FCKpd___198
___FCKpd___199
___FCKpd___200
___FCKpd___201
___FCKpd___202
___FCKpd___203
___FCKpd___204
___FCKpd___205
___FCKpd___206
___FCKpd___207
___FCKpd___208
___FCKpd___209
___FCKpd___210
___FCKpd___211
___FCKpd___212
___FCKpd___213
___FCKpd___214
___FCKpd___215
___FCKpd___216
___FCKpd___217
___FCKpd___218
___FCKpd___219
___FCKpd___220
___FCKpd___221
___FCKpd___222
___FCKpd___223
___FCKpd___224
___FCKpd___225
___FCKpd___226
___FCKpd___227
___FCKpd___228
___FCKpd___229
___FCKpd___230
___FCKpd___231
___FCKpd___232
___FCKpd___233
___FCKpd___234
___FCKpd___235
___FCKpd___236
___FCKpd___237
___FCKpd___238
___FCKpd___239
___FCKpd___240
___FCKpd___241
___FCKpd___242
___FCKpd___243
___FCKpd___244
___FCKpd___245
___FCKpd___246
___FCKpd___247
___FCKpd___248
___FCKpd___249
___FCKpd___250
___FCKpd___251
___FCKpd___252
___FCKpd___253
___FCKpd___254
___FCKpd___255
___FCKpd___256
___FCKpd___257
___FCKpd___258
___FCKpd___259
___FCKpd___260

    

你可能感兴趣的:(数据结构,算法,null,存储,merge,终端)