#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Nameval Nameval;
struct Nameval{
char *name;
int value;
Nameval *next;
};
Nameval *newLinkList(char *name,int value)
{
Nameval *newp=(Nameval *)malloc(sizeof(Nameval));
newp->name = name;
newp->value = value;
newp->next=NULL;
return newp;
}
Nameval *delItem(Nameval *listp,char *name)
{
Nameval *p,*prev;
prev=NULL;
for(p=listp;p=NULL;p=p->next)
{
if(strcmp(name,p->name)==0){
if(prev==NULL)
listp=p->next;
else
prev->next=p->next;
free(p);
return listp;
}
prev=p;
}
return NULL;
}
Nameval *addFront(Nameval *listp,Nameval *newp)
{
newp->next=listp;
return newp;
}
Nameval *addEnd(Nameval *listp,Nameval *newp)
{
Nameval *p;
if(listp==NULL)
return newp;
for(p=listp;p->next!=NULL;p=p->next)
;
p->next=newp;
return listp;
}
/*lookup: sequential search for name in listp*/
Nameval *lookup(Nameval *listp,char *name)
{
for(;listp!=NULL;listp=listp->next)
if(strcmp(listp->name,name)==0)
return listp;
return NULL; /*no match*/
}
void apply(Nameval *listp,void (*fn)(Nameval* ,void*),void *arg)
{
for(;listp!=NULL;listp=listp->next)
(*fn)(listp,arg);
}
void printnv(Nameval *p,void *arg)
{
char *fmt;
fmt=(char *)arg;
printf(fmt,p->name,p->value);
}
void inccounter(Nameval *p,void *arg)
{
int *ip;
/*p is unused*/
ip=(int *)arg;
(*ip)++;
}
void freell(Nameval *listp)
{
Nameval *next;
for(;listp!=NULL;listp=next)
{
next=listp->next;
free(listp);
}
}
//递归翻转
Nameval *reverse2(Nameval *pList, Nameval *pPre){
if (pList == NULL){
return pPre;
}
Nameval *pHead = reverse2(pList->next, pList);
pList->next = pPre;
return pHead;
}
//非递归翻转
Nameval* reverse(Nameval* pHead)
{
Nameval* pReversedHead = NULL; //初始状态,为空表
Nameval* pNode = pHead; //初始状态,原链表待逆转的结点为第一个结点--》头结点
Nameval* pPrev = NULL; //初始状态,头结点前驱结点为NULL
while(pNode != NULL)
{
// get the next node, and save it at pNext
Nameval* pNext = pNode->next;
// if the next node is null, the currect is the end of original
// list, and it's the head of the reversed list
if(pNext == NULL)
pReversedHead = pNode;
// reverse the linkage between nodes
pNode->next = pPrev;
// move forward on the the list
pPrev = pNode;
pNode = pNext;
}
return pReversedHead;
}
int main()
{
Nameval *listp=newLinkList("sunho",1);
Nameval *listp1=newLinkList("cc",2);
Nameval *listp2=newLinkList("gg",3);
listp=addFront(listp,listp1);
listp=addEnd(listp,listp2);
Nameval *listp3=lookup(listp,"sunho");
printf("%s\n",listp3->name);
//if without "const_cast<char*>" ,else :error: invalid conversion from 'const void*' to 'void*'
apply(listp,printnv,const_cast<char*>("%s %d\n"));//
// 产生如下错误
//(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
//解决方法用 gcc listp.cpp -lstdc++
//出现这个错误:是因为你用gcc编译.cpp文件.按系统默认.cpp文件是c++的文件格式
//另一个方法是用g++ listp.cpp 也是可以的
//还有一种方法是把文件保存为.c格式,反正里面全是c的代码
//然后用gcc listp.c或者是g++ listp.c 都是OK的
int n=0;
apply(listp,inccounter,&n);
printf("%d elements in listp\n",n);
// freell(listp);
//apply(listp,printnv,const_cast<char*>("%s %d\n"));
listp=reverse2(listp,NULL);
apply(listp,printnv,const_cast<char*>("%s %d\n"));
return 0;
}
单向链表
最新推荐文章于 2024-09-09 21:22:46 发布