循环链表实验

 

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

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

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

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值