voip
风的方向
厚德致远,博学敦行!
posts - 52,comments - 21,trackbacks - 0
一言难尽啊。。。
最近发现一个怪现象。。。浙大门口斑马线的红绿灯是只显示红灯的。。。绿灯的时候是全黑的。。。难道是培养耐心!!!
Link.cpp文件(这个文件主要用来测试链表增删查改函数):

#include<stdio.h>
#include
"link.h"
int main()
{
    
int i;

    ptonode L[
100],*L0;

    List l;

    initlist(
&l);//函数初始化

    
for(i=10;i>=0;i--)//appendnode函数
        appendnode(&l,i);

    printlist(
&l);//printlist函数
    
/*    sortlist(&l,L);

    for(i=0;i<NodeCount(&l);i++)
        printf("%d ",L[i]->data);
    printf("\n");
*/


    L0
=sortlist0(&l);
    
for(i=0;i<NodeCount(&l);i++)
        printf(
"%d ",L0[i]->data);
    printf(
"\n");
//    printf("\n");
    
/*    int Insertnumber,Index;//insertnode函数测试
    while(1)
    {
        printf("Insertnumber and Index:\n");
        scanf("%d %d",&Insertnumber,&Index);

        insertnode(&l,Index,Insertnumber);
        printlist(&l);
    }

    int deletenumberindex;
    while(1)
    {
        printf("deletenumberindex:\n");
        scanf("%d",&deletenumberindex);

        deletenode(&l,deletenumberindex);
        printlist(&l);
    }
*/


/*    int *p;//地址值++是加上指针所指向数据类型大小
    int a[10]={0,1,2,3,4,5,6,7,8,9};
    p=a;
    printf("%d\n",p);
    p++;
    printf("%d\n",p);
    return 0;
*/

    
return 0;
}


Link1.cpp文件(函数定义):
#include<stdio.h>
#include
"link.h"
#include
<stdlib.h>
#include
<string.h>
static ptonode locatepos(List *l,int index)//查找节点。。
{
    ptonode pnode
=*l;
    
int i=0;
    
while(pnode!=NULL)
    
{
        i
++;
        
if(i==index)
            
return pnode;
        pnode
=pnode->next;
    }

    
return pnode;
}



ptonode findnode(List
* l,elementtype data)//查找节点
{
    ptonode pnode
=*l;
    
while(pnode!=NULL)
    
{
        
if(pnode->data=data)
            
return pnode;
        pnode
=pnode->next;
    }

    
return pnode;
}


static ptonode createnode(elementtype data)//构建一个节点
{
    ptonode pnode;
    pnode
=(ptonode)malloc(sizeof(Node));
    pnode
->data=data;
    pnode
->next=NULL;
    
return pnode;
}


ptonode findlast(List
* l)//查找最后一个节点
{
    ptonode pnode
=*l;
    
if(*l==NULL)
      
return *l;
    
while(pnode->next!=NULL)
    pnode
=pnode->next;
     
return pnode;
}


void initlist(List* l)//初始化虽然只有一句代码,但是指针作为参数传递需要掌握。。。而且这里比较很难理解
{
//to-do
*l=NULL;
}

void appendnode(List* l,elementtype data)//最后位置插入节点
{
  
//to-do
  ptonode pnode,pprev;
  pnode
=createnode(data);
  pprev
=findlast(l);
  
if(pprev==NULL)
    
*l=pnode;
  
else
    pprev
->next=pnode;   
}


int NodeCount(List *l)//节点计数
{
    ptonode pnode
=*l;
    
int count=0;
    
while(pnode!=NULL)
    
{
        count
++;
        pnode
=pnode->next;
    }

    
return count;
}


void insertnode(List* l,int index,elementtype data)//在第index后面插入一个节点
{
//    int i;
    int count;
    count
=NodeCount(l);
    
if(index<0||index>count)
    
{
        printf(
"Wrong insert position!index should in 0-%d\n",count);
        
return;
    }

    ptonode pnode
=NULL,pprev=*l,pprevnext=NULL;
//    pnode=(ptonode)malloc(sizeof(Node));
    pnode=createnode(data);

    
if(index==0)
    
{
        pnode
->next=*l;
        
*l=pnode;
        
return;
    }


    pprev
=locatepos(l,index);
    pnode
->next=pprev->next;
    pprev
->next=pnode;
    
/*
    for(i=1;i<index;i++)
      pprev=pprev->next;
    //pprev->next=ptonode;
    pprevnext=pprev->next;
    pprev->next=pnode;
    pnode->next=pprevnext;
*/

}


void deletenode(List* l,int index)//删除第index个节点
{
    
int count;
    count
=NodeCount(l);
    
if(*l==NULL||index<=0||index>count)
    
{
        printf(
"Wrong insert position!index should in 0-%d\n",count);
        
return ;
    }


    ptonode pprev
=*l,pnode=NULL,fnode;

    
if(index==1)
    
{
        
*l=pprev->next;
        free(pprev);
        
return;
    }

    
int i;
    
for(i=1;i<index-1;i++)
        pprev
=pprev->next;
    
    fnode
=pprev->next;
    pnode
=fnode->next;
    free(fnode);
    pprev
->next=pnode;
    
return;
}




void editnode(List* l,int index,elementtype data)
{
    ptonode pnode;
    pnode
=locatepos(l,index);
    pnode
->data=data;
}



void sortlist(List *l,ptonode L[])//从小到大排序。。数组方式实现排序。。。局限性很大
{
    
int count;
    
int i,j;
    count
=NodeCount(l);
    ptonode  head;
    
    head
=*l;

    
for(i=0;i<count;i++)
    
{
        L[i]
=head;
        head
=head->next;
    }


    
     ptonode  temp; 
//交换排序。。
     for(i=0;i<count-1;i++)
     
{
        temp
=L[i];
        
for(j=i+1;j<count;j++)
        
{
            
if(L[i]->data>L[j]->data)
            
{
                temp
=L[i];
                L[i]
=L[j];
                L[j]
=temp;
            }

        }

     }

/*    L=(ptonode)malloc(count*sizeof(ptonode));

    for(i=0,pnode=L;i<count;i++,pnode++)//地址值++是加上指针所指向数据类型大小
    {
        pnode=head;
        head=head->next;
    }
    
     ptonode  temp; //交换排序。。
     for(i=0;i<count-1;i++)
     {
        temp=&L[i];
        for(j=i+1;j<count;j++)
        {
            if(L[i].data>L[j].data)
            {
                temp=&L[i];
                &L[i]=&L[j];
                &L[j]=temp;
            }
        }
     }
*/

//     return L;
}



ptonode  
*sortlist0(List *l)//动态开辟内存实现数组排序。。。开心了。。。
{
    
int i,j,count;
    ptonode 
*L,*pnode,head;
    head
=*l;
    count
=NodeCount(l);
    L
=(ptonode *)malloc(count*sizeof(ptonode));//这里是一个指针的指针。。。这。。

    
for(i=0,pnode=L;i<count;i++,pnode++)//地址值++是加上指针所指向数据类型大小
    {
        
*pnode=head;
        head
=head->next;
    }

    
     ptonode  temp; 
//交换排序。。
     for(i=0;i<count-1;i++)
     
{
        temp
=L[i];
        
for(j=i+1;j<count;j++)
        
{
            
if(L[i]->data>L[j]->data)
            
{
                temp
=L[i];
                L[i]
=L[j];
                L[j]
=temp;
            }

        }

     }

    
return L;
}

void emptylist(List *l)//清空
{
    ptonode pnode
=*l,fnode;
    
if(!pnode)
        
return;
    
else
        
while(pnode!=NULL)
        
{
            fnode
=pnode;
            pnode
=pnode->next;
            free(fnode);
        }

    
return ;
}


void printlist(List *l)//打印
{
  ptonode pnode
=*l;
  
while(pnode!=NULL)
  
{
    printf(
"%d ",pnode->data);
    pnode
=pnode->next;
  }

  printf(
"\n");
}




Link.h头文件(这个文件主要用来定义数据结构,以及函数申明):
//#ifndef LIST_H
//#define LIST_H
typedef int elementtype;

struct Node
{
    elementtype  data;
    
struct Node *next;
}
;

typedef 
struct Node Node;
typedef Node
* ptonode;
typedef Node
* List;


void initlist(List* l);
void appendnode(List* l,elementtype data);
void insertnode(List* l,int index,elementtype data);
void deletenode(List* l,int index);
void editnode(List* l,int index,elementtype data);
ptonode findnode(List
* l,elementtype data);
void sortlist(List* l,ptonode L[]);
ptonode 
*sortlist0(List* l);
void emptylist(List *l);
void printlist(List *l);
int NodeCount(List *l);
posted on 2010-10-31 20:05 jince 阅读(229) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


哈哈哈哈哈哈