关闭

循环链表实验

217人阅读 评论(0) 收藏 举报

 

<iframe style="WIDTH: 89.81%; HEIGHT: 64px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750500&amp;lmt=1192819750&amp;prev_fmts=468x60_as&amp;format=468x15_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

<iframe style="WIDTH: 44.33%; HEIGHT: 259px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCB94-nvUZWENUB9QugJQtwRQTVAgULcCUB4&amp;oe=gb2312&amp;dt=1192720966468&amp;lmt=1192720966&amp;format=250x250_as&amp;output=html&amp;correlator=1192720966453&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-67-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192720940&amp;ga_hid=1155529757&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>一、单向链表的存储表示

C源程序

#include<stdio.h>
#include<malloc.h>
#include<conio.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 *next;
};

typedef struct LNODE LNode;
typedef struct LNODE *LinkList;


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

int ListLength(LinkList L)
{
  int j=0;
  while (L->next)
    {
      L=L->next;
      j++;
    }
  return j;
}

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;
}

int EqualList(ElemType *e1,ElemType *e2)
{
  if (strcmp(e1->name,e2->name)==0)
    return 1;
  else
    return 0;
}

int Less_EqualList(ElemType *e1,ElemType *e2)
{
  if (strcmp(e1->name,e2->name)<=0)
    return 1;
  else
    return 0;
}
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;pc=pa;pa=pa->next;
        }
      else
        {
          pc->next=pb;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;
  p->next=s;
  return OK;
}/*ListInsert Before i */


main()
{
  struct STU e;
  LinkList La,Lb,Lc;

  clrscr();

  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();

  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");

}


二、单向链表的基本操作

C源程序

#include<stdio.h>
#include<malloc.h>
#include<conio.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 *next;
};

typedef struct LNODE LNode;
typedef struct LNODE *LinkList;


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

int ListLength(LinkList L)
{
  int j=0;
  while (L->next)
    {
      L=L->next;
      j++;
    }
  return j;
}

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;
}

int EqualList(ElemType *e1,ElemType *e2)
{
  if (strcmp(e1->name,e2->name)==0)
    return 1;
  else
    return 0;
}

int Less_EqualList(ElemType *e1,ElemType *e2)
{
  if (strcmp(e1->name,e2->name)<=0)
    return 1;
  else
    return 0;
}
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;pc=pa;pa=pa->next;
        }
      else
        {
          pc->next=pb;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;
  p->next=s;
  return OK;
}/*ListInsert Before i */


main()
{
  struct STU e;
  LinkList La,Lb,Lc;

  clrscr();

  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();

  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");
}

<iframe style="WIDTH: 52.39%; HEIGHT: 263px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCKcC4yuPlq5lUDRQDVAtUK4BUENQCA&amp;oe=gb2312&amp;dt=1192819388296&amp;lmt=1192819388&amp;format=300x250_as&amp;output=html&amp;correlator=1192819388281&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fviewthread.php%3Ftid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fpost.php%3Faction%3Dedit%26fid%3D16%26tid%3D70%26pid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1527165455&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=5&amp;u_java=true"></iframe>

<iframe style="WIDTH: 82.23%; HEIGHT: 74px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192818497343&amp;lmt=1192818497&amp;format=468x60_as&amp;output=html&amp;correlator=1192818497343&amp;channel=1741427766&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforumdisplay.php%3Ffid%3D16%26page%3D1&amp;ui=rc%3A6&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1000484876&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=6&amp;u_java=true"></iframe>

<iframe style="WIDTH: 58.41%; HEIGHT: 156px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750656&amp;lmt=1192819750&amp;prev_fmts=468x60_as%2C468x15_0ads_al_s%2C234x60_as&amp;format=200x90_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe> 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场