zoukankan      html  css  js  c++  java
  • 双向链表

    #include<stdio.h>
    #include<malloc.h>

    #define ERROR 0
    #define OK 1
    #define EQUAL 1
    #define OVERFLOW -1
    #define LIST_INIT_SIZE 100
    #define LISTINCREMENT 10

    struct STU{
      char name[20];
      char stuno[10];
      int age;
      int score;
    }stu[50];
    typedef struct STU ElemType;

    struct LNODE
    {
      ElemType data;
      struct LNODE *prior;
      struct LNODE *next;
    };

    typedef struct LNODE LNode;
    typedef struct LNODE *LinkList;

    /*初始化链表L*/
    int init(LinkList *L)
    {
      *L=(LNode *)malloc(sizeof(LNode));
      if(!L)   exit(ERROR);
      (*L)->next=NULL;
      (*L)->prior=NULL;
      return OK;
    }/*init */

    /*返回链表长度*/
    int ListLength(LinkList L)
    {
      int j=0;
      while (L->next)
        {
          L=L->next;
          j++;
        }
      return j;
    }

    /*获得链表L的第i个元素,并赋值给指针e*/
    int GetElem(LinkList L,int i,ElemType *e)
    {
      LinkList p; int j;
      p=L->next;j=1;
      while(p&&j<i){
        p=p->next;++j;
      }
      if(!p||j>1)  return ERROR;
      *e=p->data;
      return OK;
    }

    /*判断e1与e2两元素是否相等*/
    int EqualList(ElemType *e1,ElemType *e2)
    {
      if (strcmp(e1->name,e2->name)==0)
        return 1;
      else
        return 0;
    }

    /*比较元素e1与e2*/
    int Less_EqualList(ElemType *e1,ElemType *e2)
    {
      if (strcmp(e1->name,e2->name)<=0)
        return 1;
      else
        return 0;
    }

    /*从线性链表L中删除第i个元素*/
    int DeleteElem(LinkList L,int i)
    {
        LinkList p;  int j;
        j=0;
        p=L;
        while (p->next && j<i){
          p=p->next; ++j;
        }
        if(!p)  return ERROR;
        p->prior->next=p->next;
        p->next->prior=p->prior;
        free(p);
        return OK;

    /*元素e是否在链表La中*/
    int LocateElem(LinkList La,ElemType e,int type)
    {
      int i;
      LinkList p;
      p=La;
      switch (type)
        {
          case EQUAL:
       while(p->next)
         {
           p=p->next;
           if(EqualList(&p->data,&e))
            return 1;
         }
       return 0;
     break;
          default:
     break;
        }
      return 0;
    }

    void MergeList(LinkList La,LinkList Lb,LinkList *Lc)
    {
      LinkList pa,pb,pc;
      pa=La->next;pb=Lb->next;
      *Lc=pc=La;
      while(pa && pb)
        {
          if(Less_EqualList(&pa->data,&pb->data))
     {
       pc->next=pa;pa->prior=pc;pc=pa;pa=pa->next;
     }
          else
     {
       pc->next=pb;pb->prior=pc;pc=pb;pb=pb->next;
     }
        }
      pc->next=pa?pa:pb;
      free(Lb);
    }

    int printlist(LinkList L)
    {
      int i;
      LinkList p;
      p=L;
      printf("name       stuno        age     score\n");
      while(p->next)
        {
          p=p->next;
          printf("%-10s %s\t%d\t%d\n",  p->data.name,  p->data.stuno,
       p->data.age,  p->data.score);
        }
      printf("\n");
    }

    int ListInsert(LinkList L,int i,ElemType e)
    {
      LinkList p,s;
      int j;
      p=L;j=0;
      while(p&&j<i-1)
        {
          p=p->next;
          ++j;
        }
      if(!p||j>i-1) return ERROR;
      s=(LinkList)malloc(sizeof(LNode));
      s->data=e;
      s->next=p->next;
      s->prior=p;
      p->next=s;
      return OK;
    }/*ListInsert Before i */


    int main()
    {
      struct STU e,t,*p;
      LinkList La,Lb,Lc;

      printf("\n\n-------------------List Demo is running...----------------\n\n");
      printf("First is InsertList function.\n");
      init(&La);

      strcpy(e.name,"stu1");
      strcpy(e.stuno,"100001");
      e.age=80;
      e.score=1000;
      ListInsert(La,1,e);
      strcpy(e.name,"stu3");
      strcpy(e.stuno,"100002");
      e.age=80;
      e.score=1000;
      ListInsert(La,2,e);

      printlist(La);
      getch();

      strcpy(e.name,"stu5");
      strcpy(e.stuno,"100003");
      e.age=80;
      e.score=1000;
      ListInsert(La,3,e);
     
      printlist(La);
      getch();
     
      printf("Test DeleteElem\n");
      DeleteElem(La,1);
      printlist(La);
      getch();

      init(&Lb);

      strcpy(e.name,"stu2");
      strcpy(e.stuno,"100001");
      e.age=80;
      e.score=1000;
      ListInsert(Lb,1,e);
      strcpy(e.name,"stu4");
      strcpy(e.stuno,"100002");
      e.age=80;
      e.score=1000;
      ListInsert(Lb,2,e);

      strcpy(e.name,"stu6");
      strcpy(e.stuno,"100001");
      e.age=80;
      e.score=1000;
      ListInsert(Lb,3,e);

      printlist(Lb);
      getch();

      MergeList(La,Lb,&Lc);
      printlist(Lc);
      getch();

      printf("\n\n\n\n\n\nWelcome to visit http://zmofun.heha.net !\n\n\n\n\n\n\n");
      getch();
      return 0;
    }

  • 相关阅读:
    English trip -- VC(情景课)9 A Get ready
    English trip -- Review Unit8 Work 工作
    English trip -- VC(情景课)8 D Reading
    bzoj 4238 电压
    luoguP2154 [SDOI2009]虔诚的墓主人
    bzoj 2225 [Spoj 2371]Another Longest Increasing
    bzoj 4383 [POI2015]Pustynia
    luogu3706 [SDOI2017]硬币游戏
    luogu P6125 [JSOI2009]有趣的游戏
    luogu4443 coci 2017 Dajave
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035179.html
Copyright © 2011-2022 走看看