线性表的链式表示与实现,线性表链式






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

#define ERROR 0
#define OK 1
#define EQUAL 1
#define LESSEQUAL 2
#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)
...{
    
/**//*
    int init(LinkList *L)
    功能:初始化一个列表.
    参数:*L ->输出
    返回:成功为1,失败为0
    
*/


    
*L=(LNode *)malloc(sizeof(LNode));//分配内存空间
    if(!L)   
        exit(ERROR);                
//分配失败

    (
*L)->next=NULL;                    //让它的Next=0,即为头节点
    return OK;                            //返因成功
}
/**//*init */

int ListLength(LinkList L)
...{
    
/**//*int ListLength(LinkList L)
    功能:取某列表的长度
    参数:L (列表)
    返回:L 列表的当前实际长度
    
*/


    
int j=0;    //初始计数
    while (L->next)//有next 证明非尾节点
    ...{
        L
=L->next; //使自身往后移->指向下一个节点
        j++;        //计数
    }

    
return j;
}


int GetElem(LinkList L,int i,ElemType *e)
...{
    
/**//*int GetElem(LinkList L,int i,ElemType *e)
    功能:取得列表中指定位置的元素
    参数:L 要操作的列表
    参数:i 位置
    参数:*e ->输出
    返回:成功为1,失败为0
    
*/


    LinkList p; 
    
int j;

    p
=L->next;//使P指向头指针的下一个(即第一个) 这里用参数L 指的是结构体
    j=1;
    
    
while(p&&j<i)//循环P为真且j<i,目的是将p指向位置为i的元素
    ...{
        p
=p->next;
        
++j;
    }


    
if(!p||j>1//取值错误
        return ERROR;

    
*e=p->data;
    
return OK;
}


int EqualList(ElemType *e1,ElemType *e2)
...{
    
/**//*int EqualList(ElemType *e1,ElemType *e2)
    功能:比较两个列表元素
    参数:*e1 要比较之一
    参数:*e2 参数2
    返回:如果两个元素的名字相等 返回1 否则返回0
    
*/


    
if (strcmp(e1->name,e2->name)==0)
        
return 1;
    
else
        
return 0;
}


int Less_EqualList(ElemType *e1,ElemType *e2)
...{
    
/**//*int Less_EqualList(ElemType *e1,ElemType *e2)
    功能:比较两个元素是否第一个小于第二个
    返回:若第一个元素比第二个小,返回1 否则返回0
    
*/

    
if (strcmp(e1->name,e2->name)<=0)
        
return 1;
    
else
        
return 0;
}

int LocateElem(LinkList La,ElemType e,int type)
...{
    
/**//*int LocateElem(LinkList La,ElemType e,int type)
    功能:查找某个元素是否在列表中
    参数:La 列表
    参数:e 元素
    参数:type 类型(在此例中为多余)
    返回:如果存在返回所在的位置,否则返回0
    
*/

    
int i;
    LinkList p;
    p
=La;
    
switch (type)
    
...{
        
case EQUAL:
            
while(p->next)
            
...{
                p
=p->next;
                
++i;
                
if(EqualList(&p->data,&e))
                    
return i;
            }

            
return 0;
            
break;
        
default:
            
break;
    }

    
return 0;
}


void MergeList(LinkList La,LinkList Lb,LinkList *Lc)
...{
    
/**//*void MergeList(LinkList La,LinkList Lb,LinkList *Lc)
    功能:并归两个列表并生成一个新的列表
    参数:La 列表a
    参数:Lb 列表b
    参数:*Lc ->输出 列表
    返回:无
    
*/

    
    LinkList pa,pb,pc;
//定义三个列表变量
    pa=La->next;        //使pa指向列表a的第一个元素
    pb=Lb->next;        //使pb指向列表b的第一个元素

    
*Lc=pc=La;            //使Pc=La再使*Lc=pc 完成 输出参数的初始化
    
    
while(pa && pb)        //循环
    ...{
        
if(Less_EqualList(&pa->data,&pb->data))//如果列表pa指向的元素小于pb指向的元素
        ...{
            pc
->next=pa;//使pc的下位指向pa
            pc=pa;        //使pc=pa
            pa=pa->next;//使pa下移一个位置
        }

        
else
        
...{
            
//操作同上 换作pb
            pc->next=pb;pc=pb;pb=pb->next;
        }

    }

    pc
->next=pa?pa:pb;
    free(Lb);
//释放Lb
}


//LinkList UnionList(LinkList La,LinkList Lb)
//{
//    LinkList pa,head;
//    
//    pa=head=La;
//    
//    printf("head is:%ld %ld ",head,La);
//    
//    while(pa->next !=0)
//    {
//        pa=pa->next ;
//        printf(" %d ",pa);    
//    }
//    
//    pa->next=Lb->next;
//    
//    return head;
//
//}

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

    printf(
" ");
}


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,Ld;

    
//  clrscr(); //清屏 vc6不支持此函数

    printf(
" -------------------线性列表的链式表示演示 运行中...---------------- ");
    printf(
"第一个 演示插入功能. ");
    init(
&La);

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

    printlist(La);
    getch();

    strcpy(e.name,
"学生5");
    strcpy(e.stuno,
"100003");
    e.age
=80;
    e.score
=1000;
    ListInsert(La,
3,e);

    printlist(La);
    getch();

    init(
&Lb);

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

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

    printlist(Lb);
    getch();

//    printf("联合列表... ");
//    Ld=UnionList(La,Lb);
//     printlist(Ld);
//    getch();
    
    
    printf(
"并归列表... ");
    MergeList(La,Lb,
&Lc);
    printlist(Lc);
    getch();
    
    
    
    printf(
" 欢迎 加入程序员群:29940046 ");
    getch();
    
}



 
 
插入操作 模拟

Status ListInsert_L(LinkList &L,int i,ElemType e){

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_L



插入



删除操作 模拟实现

Status ListDelete_L(LinkList &L,int i,ElemType &e){

p=L,j=0;

while(p&&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;

}//ListDelete_L


 

删除

www.xyjys.comtrue/article/20231129/2444266.htmlTechArticle线性表的链式表示与实现,线性表链式 #include stdio.h #include malloc.h #include conio.h #define ERROR0 #define OK1 #define EQUAL1 #define LESSEQUAL2 #define OVERFLOW-1 #define LIST_INIT_SIZE100 #define LISTINCREMENT10 struct...

相关文章

    暂无相关文章

小鱼文聚评论

XY推荐