千里暮云平

常用链接

统计

最新评论

数据结构代码 注意前移和后移的区别,对于数组来说

 //顺序表
// 线性表的动态分配顺序存储结构

#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 阅读(324) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理