Created
October 9, 2014 07:06
-
-
Save JesseEisen/e4febaf447f73cb3fd39 to your computer and use it in GitHub Desktop.
A easy way to contruct a linklist and some base options. written by C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define NLEN 5 | |
#define NVAL 5 | |
typedef struct Nameval Nameval; | |
typedef void(*func)(Nameval *, void *); | |
struct Nameval | |
{ | |
char *name; | |
int value; | |
Nameval *next; | |
}; | |
Nameval *newitem(char *name, int value) | |
{ | |
Nameval *newp; | |
newp = (Nameval *)malloc(sizeof(Nameval)); | |
if(newp == NULL) | |
exit(1); | |
newp->name = name; | |
newp->value = value; | |
newp->next = NULL; | |
return newp; | |
} | |
Nameval *addend(Nameval *listp, Nameval *items) | |
{ | |
Nameval *p; | |
if(listp == NULL) | |
return items; | |
for(p = listp; p->next != NULL; p = p->next) | |
; | |
p->next = items; | |
return listp; | |
} | |
//print list items | |
void printnv(Nameval *listp, void *arg) | |
{ | |
char *fmt; | |
fmt =(char *)arg; | |
printf(fmt,listp->name,listp->value); | |
} | |
void apply(Nameval *listp,func fp,void *arg) | |
{ | |
for(;listp!= NULL; listp = listp->next) | |
fp(listp,arg); | |
printf("\n"); | |
} | |
//loop to revese a list | |
Nameval *revese(Nameval *listp) | |
{ | |
Nameval *current, *p; | |
if(listp == NULL) | |
return NULL; | |
current = listp->next; //point to first item | |
while(current->next != NULL) | |
{ | |
p = current ->next; //point to second item | |
current->next = p->next; //make first item point to thrid | |
p->next = listp->next; //make second item point to oldfirst | |
listp->next = p; //reset the new first item | |
} | |
return listp; | |
} | |
Nameval *delitem(Nameval *listp, char *name) | |
{ | |
Nameval *prev, *p; | |
prev = NULL; | |
for(p = listp->next; p != NULL;p = p->next) | |
{ | |
if(strcmp(name, p->name) == 0) | |
{ | |
if(prev == NULL) | |
listp->next = p->next; | |
else | |
prev->next = p->next; | |
free(p); | |
return listp; | |
} | |
prev = p; //save the previous item. | |
} | |
printf("delitem:%s can't find\n",name); | |
return NULL; | |
} | |
Nameval *insertitem(Nameval *listp, char *name,Nameval *newp) | |
{ | |
Nameval *p; | |
for(p = listp->next; p != NULL; p= p->next) | |
{ | |
if(strcmp(name, p->name) == 0) | |
{ | |
newp->next = p->next; | |
p->next = newp; | |
return listp; | |
} | |
} | |
printf("insertitem:%s can't find\n",name); | |
return NULL; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
Nameval *item,*nlist; | |
//init the head pointer | |
nlist = (Nameval *)malloc(sizeof(Nameval)); | |
//if(nlist == NULL) | |
// exit(0); | |
//nlist->next = NULL; | |
char *name[NLEN] = {"jesse","sky","ward","may","simmons"}; | |
int num[NVAL]={2,3,1,5,4}; | |
int i; | |
//loop to create a list | |
for(i = 0; i < 5; i++) | |
{ | |
nlist = addend(nlist,newitem(name[i],num[i])); | |
} | |
apply(nlist,printnv,"%s:%d "); | |
nlist = revese(nlist); | |
apply(nlist,printnv,"%s:%d "); | |
nlist = delitem(nlist,"may"); | |
apply(nlist,printnv,"%s:%d "); | |
nlist = insertitem(nlist,"sky",newitem("fize",6)); | |
apply(nlist,printnv,"%s:%d "); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment