一言難盡啊。。。
最近發現一個怪現象。。。浙大門口斑馬線的紅綠燈是只顯示紅燈的。。。綠燈的時候是全黑的。。。難道是培養耐心!!!
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 閱讀(254)
評論(0) 編輯 收藏 引用