| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 506 人关注过本帖
标题:链表的操作呢
取消只看楼主 加入收藏
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:0 
链表的操作呢
#include<stdio.h>
#include<stdlib.h>
#define OK              1
#define OVERFLOW        0
#define ERROR           0
#define NULL            0
 
typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode,*LinkList;
 
/*顺序创建一个链表*/
LinkList CreateListOrdinal_L(int num)
{
    LinkList h,head,end;
    int counter;
    if(!(h = head = (LinkList)malloc(sizeof(LNode))))
    {
        return NULL;
    }
    h->next = head->next = NULL;
    for(counter=0;counter<num;counter++)
    {
        if(end = ((LinkList)malloc(sizeof(LNode))))
        {
            end->data = rand();
            end->next = h->next;
            h->next = end;
            h = end;
        }
    }
    return head;
}
 
/*逆序创建一个链表*/
LinkList CreateList_L(int num)
{
    LinkList head,end;
    int counter;
    if(!(head=(LinkList)malloc(sizeof(LNode))))
    {
        return NULL;
    }
    head->next = NULL;
    for(counter=0;counter<num;counter++)
    {
        if(end=(LinkList)malloc(sizeof(LNode)))
        {
            end->data = rand();
            end->next = head->next;
            head->next = end;
        }
    }
    return head;
}
 
/*获取某个指定结点的值*/
int GetElement_L(LinkList list,int location)
{
    int counter = 0;
    LinkList point = list->next;
    while(point && counter<location)
    {
        point = point->next;
        counter++;
    }
    if(!point || counter > location)
        return ERROR;
    return point->data;
}
 
/*插入元素*/
int ListInsert_L(LinkList list,int location,int arg)
{
    LinkList point=list->next,node;
    int counter=1;
    while(counter<location)
    {
        point = point->next;
        counter++;
    }
    if(!point || (counter > location))
    {
        printf("\nInsert operatoin failed");
        return ERROR;
    }
    node = (LinkList)malloc(sizeof(LNode));
    if(!node)
        exit(OVERFLOW);
    node->data = arg;
    node->next = point->next;
    point->next = node;
   
    return OK;
}
 
/*删除元素*/
void ListDelete_L(LinkList list,int location)
{
    LinkList point = list->next;
    int counter = 1;
    while(counter < location)
    {
        point = point->next;
        list = list->next;
        counter++;
    }
    if(!point || (counter > location))
    {
        printf("\nDelete operation failed!\n");
        return;
    }
    list->next = point->next;
    free(point);
}
 
/*快速排序*/
void BubbleSort(LinkList list)
{
    int temp;
    LinkList pa,pb;
    pa = list->next;
    pb = pa->next;
    while(pa->next)
    {
        if(pa->data > pb->data)
        {
            temp = pa->data;
            pa->data = pb->data;
            pb->data = temp;
        }
        pb = pb->next;
        if(!pb)
        {
            pa = pa->next;
            pb = pa->next;
        }
    }
}
 
/*获取链表元素个数*/
int GetListLength_L(LinkList list)
{
    int counter=0;
    LinkList point = list->next;
    while(point)
    {
        counter++;
        point = point->next;
    }
    return counter;
}
 
/*合并两个链表,并将操作结果保留在第一个链表中。此操作结束后就只剩一个链表了*/
void MergeList_L(LinkList listA,LinkList listB)
{
    LinkList pa = listA,pb = listB;
    listA = listA->next;
    listB = listB->next;
    while(listA && listB)
    {
        if(listA->data < listB->data)
        {
            pa->next = listA;
            pa = listA;
            listA = listA->next;
        }
        else
        {
            pa->next = listB;
            pa = listB;
            listB = listB->next;
        }
    }
    pa->next = listA?listA:listB;
    free(pb);
}
 
/*列出列表的所有元素*/
void DisplayAllElements(LinkList list)
{
    int flag=1;
    LinkList point = list->next;
    while(point)
    {
        printf("%d\t",point->data);
        point = point->next;
        if(flag++ == 5)
            printf("\n");
    }
    printf("\n");
}
 
void main()
{
    LinkList listA,listB;
   
    printf("Create LinkList A\n");
    listA = CreateListOrdinal_L(4);
    ListInsert_L(listA,4,123);
    ListDelete_L(listA,3);
    BubbleSort(listA);
    printf("After sort,the elements of listA are:\n");
    DisplayAllElements(listA);
   
    printf("Create LinkList B\n");
    listB = CreateList_L(6);
    BubbleSort(listB);
    printf("After sort,the elements of listB are:\n");
    DisplayAllElements(listB);
    MergeList_L(listA,listB);
    printf("\nAfter merge the two list together,the elements are:\n");
    DisplayAllElements(listA);
    printf("\nThe length of elements of listA is:%d",GetListLength_L(listA));
    getch();
}
有什么错误啊??大家找一下啊帮忙最好注释一下啊
搜索更多相关主题的帖子: 链表 
2010-04-21 21:22
快速回复:链表的操作呢
数据加载中...
 
   



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

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