• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            千里暮云平

            常用鏈接

            統計

            最新評論

            數據結構代碼 注意前移和后移的區別,對于數組來說

             //順序表
            // 線性表的動態分配順序存儲結構

            #define LIST_INIT_SIZE 100
            #define LISTINCREMENT 10
            typedef struct{
                 ElemType *elem;
                 int    length;
                int     listsize;
            }Sqlist;


            //初始化
            Status InitList Sq(SqList &L)
            {
                  L.elem = (ElemType *)malloc(List_INIT_SIZE*sizeof(ElemType);
                  if(!L.elem)exit(OVERFLOW);   //存儲分配失敗
                  L.length = 0;
                  L.listsize = LIST_INIT_SIZE;
                  return OK;
            }


            // 插入元素
             Status ListInsert(SqList *L,int i,ElemType e) /* 算法2.4 */
            { /* 初始條件:順序線性表L已存在,1≤i≤ListLength(L)+1 */
               /* 操作結果:在L中第i個位置之前插入新的數據元素e,L的長度加1 */
               ElemType *newbase,*q,*p;
               if(i<1||i>(*L).length+1) /* i值不合法 */
                 return ERROR;
               if((*L).length>=(*L).listsize) /* 當前存儲空間已滿,增加分配 */
               {
                 newbase=(ElemType *)realloc((*L).elem,((*L).listsize+LIST_INCREMENT)*sizeof(ElemType));
                 if(!newbase)
                   exit(OVERFLOW); /* 存儲分配失敗 */
                 (*L).elem=newbase; /* 新基址 */
                 (*L).listsize+=LIST_INCREMENT; /* 增加存儲容量 */
               }
               q=(*L).elem+i-1; /* q為插入位置 */
               for(p=(*L).elem+(*L).length-1;p>=q;--p) /* 插入位置及之后的元素右移 */
                 *(p+1)=*p;
               *q=e; /* 插入e */
               ++(*L).length; /* 表長增1 */
               return OK;
            }




            //線性順序表 (實現大部分) 12:25 ,2.6  ended at 16:57


            #include <stdio.h>
            #include <stdlib.h>

            #define LIST_INIT_SIZE 100
            #define LISTINCREMENT 10
            #define OVERFLOW 0
            #define OK 1
            #define ERROR 0
            #define ElemType char

            typedef struct{
                 ElemType *elem;
                 int    length;
                 int     listsize;
            }Sqlist;


            void InitList(Sqlist *L)
            {
             L->elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
                if(!L->elem)
              exit(OVERFLOW);
             L->length = 0;
             L->listsize = LIST_INIT_SIZE;
            }

            bool ListInsert(Sqlist *L,int i,ElemType e)
            {
             if(i<1||i>L->length+1)
              return ERROR;
            /* newbase=(ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
             if(!newbase)
              return ERROR;
             L->elem = newbase;
             L->listsize += LISTINCREMENT;

             q = L->elem+i-1; //為插入位置
                for(p=L->elem+L->length-1;p>=q;--p)
             {
              *(p+1) = *p;
             }
             *q = e;
             ++L->length;
             return OK; */

             for(int j=L->length-1;j>=i-1;j--)
             {
              L->elem[j+1] = L->elem[j];
             }
             L->elem[i-1] = e;
             L->length++ ;
             return OK;
            }

            bool ListDelete(Sqlist *L,int i)
            {
             if(i<1||i>L->length)
              return ERROR;
             for(int j=i;j<L->length;j++)
             {
              L->elem[j] = L->elem[j+1];
             }
             L->length--;
                return OK;
            }

            int LocateList(Sqlist *L,ElemType e)
            {
             int i = 1;
             while(e!=L->elem[i])
                i++;
             if(i>L->length)
              return ERROR;
             else
              return i;
            }
            void main()
            {
             
            }











            /* 網上找的,單鏈表的實現,做參考*/

             

            #include <stdio.h>
            #include <stdlib.h>
            #include <conio.h>

            typedef struct LIST
            {
                    int element;
                   LIST *next;
            }LIST;

            LIST *Init_List ();           /** 建表 **/
            int Insert_List (LIST *List, int i, int element); /** 插入元素 **/
            int Delect_List (LIST *List, int i); /** 刪除元素 **/
            LIST *Get_piout(LIST *List, int i);
            void List_List (LIST *List);           /** 打印表 **/
            char menu (void);

            int main (void)
            {
                    int i;
                      int element;
                    LIST *List;
                      char choice;

                    List = Init_List ();

                    do
                    {
                               choice = menu ();
                       
                               switch (choice)
                               {
                                       case 'i':
                                       case 'I':
                                                 printf ("i , element==>> ");
                                                scanf ("%d%d", &i, &element);
                                                 Insert_List (List, i, element);
                                                 break;
                       
                                       case 'd':
                                       case 'D':
                                                printf ("i==>> ");
                                                scanf ("%d", &i);
                                                Delect_List (List, i);
                                                break;
               
                                        case 'l':
                                        case 'L':
                                                  List_List (List);
                                                 break;
                
                                         default :
                                                 break;
                                 }
                     }while (choice != 'q' && choice != 'Q');

                     return 0;
            }

            /**建表**/
            LIST *Init_List (void)
            {
                     int i ;
                     LIST *temp, *This, *List;

                     List = (LIST *) malloc (sizeof(LIST));
                   List->next = NULL;

                      This = List;
                    /* 表頭插入法建有五個元素的表 */
                     for (i = 1; i <= 5; ++i)
                     {
                               temp = (LIST *) malloc (sizeof(LIST));
              
                               printf ("enter %d Node>>",i);
                               scanf ("%d", &(temp->element));
              
                              temp->next = NULL;
                              This->next = temp;
                             This = temp;
                    }

                     return (List);
            }

            /** 插入表 **/
            int Insert_List (LIST *List, int i, int element)
            {
                      LIST *This, *temp;

                      This = Get_piout (List, i);

                      temp = (LIST*) malloc (sizeof (LIST));

                      temp->element = element;
                      temp->next = This->next;
                      This->next = temp;

            return 0;
            }

            /*** 刪除元素 ***/
            int Delect_List (LIST *List, int i)
            {
                    LIST *This, *p;

                    This = Get_piout (List, i);

                     p = This->next;
                  This->next = p->next;
                    free (p);

                    return 0;
            }

            /**** 找位置函數 ***/
            LIST *Get_piout(LIST *List, int i)
            {
                     LIST *This;
                     int j = 0;

                    This = List;

                     while ((This != NULL)&& (j < i - 1))
                    {
                           This = This->next;
                             ++j;
                     }

                     if ((This == NULL) || (j > i - 1))
                     {
                               return NULL;
                     }

                     return (List) ;
            }

            /**** 輸出函數 ***/
            void List_List (LIST *List)
            {
                     LIST *q;

                     q = List->next;

                      if(q == NULL)
                      {
                                printf ("empty\n");
                                 getch();
                                return ;
                      }

                      while (q->next != NULL)
                      {
                                 printf ("%d,", q->element);
                                 q = q->next;
                      }
                        printf ("%d", q->element);
                      getch();

            }


            char menu (void)
            {
                     char choice;

                     system ("cls");
                        printf ("----------------------------------\n");
                     printf ("        i insert\n");
                       printf ("        d delete\n");
                     printf ("        l   list\n");
                     printf ("\n\n");
                       printf ("        q qiut\n");
                     printf ("----------------------------------\n");
                       printf ("------Your choice>>");
                       scanf ("%c", &choice);

                     fflush (stdin);
                       return (choice);
            }

            posted on 2010-02-06 17:01 Adam 閱讀(334) 評論(0)  編輯 收藏 引用

            好久久免费视频高清| 免费国产99久久久香蕉| 性高朝久久久久久久久久| 久久久久亚洲精品无码网址| 久久一本综合| 无码人妻久久一区二区三区蜜桃| 久久婷婷人人澡人人爽人人爱| 久久亚洲精品中文字幕| 99热精品久久只有精品| 2020国产成人久久精品| 久久久中文字幕| 亚洲日本久久久午夜精品| 人妻精品久久久久中文字幕69| 亚洲乱亚洲乱淫久久| 久久毛片一区二区| 国产亚州精品女人久久久久久| 奇米影视7777久久精品人人爽| 777米奇久久最新地址| 四虎影视久久久免费观看| 久久久青草青青亚洲国产免观| 精品久久久久久国产| 久久综合伊人77777| 9久久9久久精品| 97精品依人久久久大香线蕉97| 久久国产亚洲精品麻豆| 性色欲网站人妻丰满中文久久不卡| 久久AAAA片一区二区| 国产精品久久成人影院| 久久亚洲精精品中文字幕| 久久精品免费全国观看国产| 国内精品伊人久久久久影院对白| 久久婷婷五月综合97色| 久久精品人妻中文系列| 亚洲一级Av无码毛片久久精品| 国产午夜电影久久| 99久久精品免费看国产| 色偷偷888欧美精品久久久| 国产精品久久久久久一区二区三区| 欧美成人免费观看久久| 99久久综合国产精品免费| 99久久国产宗和精品1上映|