单链表操作

来源:互联网 发布:淘宝鞋店的装饰风格 编辑:程序博客网 时间:2024/06/06 00:05
/*************单链表的操作函数总结*****************/#include <QCoreApplication>#include <iostream>#include <stdio.h>#include <malloc.h>using namespace std;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 Less_EqualList(ElemType *e1,ElemType *e2){    if(strcmp(e1->name,e2->name) <= 0)        return 1;    else        return 0;}/*打印链表*/int printList(LinkList L){    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);    }    return 1;}/*节点初始化*/int init(LinkList *L){    //用malloc动态分配节点    *L = (LNode *)malloc(sizeof(LNode));    if(!L)     //若分配失败,返回        return 0;    //初始化数据域    memset(&((*L)->data),0,sizeof(struct STU));       //初始化指针域    (*L)->next = NULL;    return 1;}/*测试数据是否存在*/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 0;        *e = p->data;    return 1;}/*向链表中插入数据*/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 0;        s = (LinkList)malloc(sizeof(LNode));    s->data = e;        s->next = p->next;    p->next = s;    return 1;}/*从链表中删除数据*/int ListDelete(LinkList L,int i){    LinkList p;    int j;    p = L;    j = 0;    while(p && j<i-1)    {        p = p->next;        ++j;    }    if(!p || j>i-1)        return 0;    p->next = p->next->next;    p->next->data = p->next->next->data;    return 1;}/*合并两个链表*/void MergeList(LinkList La,LinkList Lb,LinkList *Lc){    /*合并单链表La和Lb到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 main(int argc, char *argv[]){    QCoreApplication a(argc, argv);        struct STU e; //定义一个data    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);    getchar();    strcpy(e.name,"stu5");    strcpy(e.stuno,"100003");    e.age = 80;    e.score = 1000;    ListInsert(La,3,e);    printList(La);    getchar();    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);    getchar();    MergeList(La,Lb,&Lc);    printList(Lc);    getchar();    return a.exec();}

0 0