标题:线性表链式存储操作实现
只看楼主
kellybinble
Rank: 1
等 级:新手上路
帖 子:18
专家分:6
注 册:2013-9-8
结帖率:80%
已结贴  问题点数:20 回复次数:1 
线性表链式存储操作实现
删除操作和查找操作不起作用,求指教。
附代码:

#include<stdio.h>
#include<malloc.h>
#include"conio.h"
#include"string.h"


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

struct STU{
  char name[20];
  char number[10];
  char sex[5];
  char kind[10];
  char address[10];
  int age;
}stu[100];
typedef struct STU ElemType;

struct LIST
{
  ElemType *elem;
  int length;
  int listsize;
};
typedef struct LIST List;

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

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

int GetElem(LinkList L,int i,struct STU *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 ListDelete(LinkList L,int i, struct STU e)
{
    LinkList p,q;
    int j;
    p=L;j=0;
  while(p->next&&j<i-1)
  {
      p=p->next;j++;
  }
  if((!p->next)||j>i-1);
  return ERROR;
  q=p->next;
  p->next=q->next;
  e=q->data;
  free(q);
  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)
{
  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;
}

int printlist(LinkList L)
{
  LinkList p;
  p=L;
  printf("name       number        sex         kind         address     age\n");
  while(p->next)
    {
        p=p->next;
      printf("%-10s %s \t%-10s\t%-10s%-10s %d\n",  p->data.name,  p->data.number,
      p->data.sex,  p->data.kind,  p->data.address,  p->data.age);
    }
  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;
}



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

   void clrscr();

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

  strcpy(e.name,"Elbert");
  strcpy(e.number,"1211");
  strcpy(e.sex,"boy");
  strcpy(e.kind,"art");
  strcpy(e.address,"tianjin");
  e.age=18;
  ListInsert(La,1,e);
  strcpy(e.name,"Joe");
  strcpy(e.number,"1212");
  strcpy(e.sex,"girl");
  strcpy(e.kind,"sport");
  strcpy(e.address,"beijing");
  e.age=17;
  ListInsert(La,2,e);

  printlist(La);
  getch();

  strcpy(e.name,"Branson");
  strcpy(e.number,"1213");
  strcpy(e.sex,"boy");
  strcpy(e.kind,"art");
  strcpy(e.address,"shanghai");
  e.age=18;
  ListInsert(La,2,e);

  printlist(La);
  getch();
  printf("First is DeletetList function.\n");


  ListDelete(La,2,e);
  printlist(La);
  getch();
  printf("First is GetList function.\n");
  GetElem(La,1,e);
  printlist(La);
  getch();



  return 0;
}
搜索更多相关主题的帖子: address include number 线性表 
2013-10-24 11:37
Osiris9
Rank: 4
来 自:九柱
等 级:业余侠客
帖 子:28
专家分:225
注 册:2013-10-25
得分:20 
删除p = p -> next
查找if(key == p -> data)

干旱时死去................丰水时重生!
2013-10-26 01:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-422437-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038218 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved