随笔 - 44  文章 - 1 评论 - 24 trackbacks - 1

QQ:363894870
MSN:xlwei_ever@hotmail.com

搜索

 

随笔分类

.net

e.studio

groups

组件设计

阅读排行榜

评论排行榜

  基本操作都写在头文件里:

 

//head.h
#include<stdio.h>
#include
<stdlib.h>
#include
<conio.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");
}
  

  主函数:

  

#include "head.h"

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

Tag标签: 算法,链表
posted on 2008-07-22 18:52 谢良威 阅读(27) 评论(0)  编辑 收藏

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: