您的位置:首页 > 其它

单链表以及基本操作-基础

2012-06-20 16:32 357 查看
文件一:node.h

#ifndef __NODE__H_

#define __NODE__H_

/*#include <stdio.h>

#include <malloc.h>

#include <memory.h>

#include <assert.h>

*/

struct Node{

int number;

char words[16];

struct Node *pnext;

};

typedef struct Node node;

node *chain_create();

int chain_add(node *head,int number,char p[]);

void chain_del(node *head,int number);

void chain_insert(node *head,int location,int number,char words[]);

int chain_find(node *head,char words[]);

void chain_output(node *head);

#endif

文件二:node.c

/*

* This file is created by Ganyue.

* The functions are below.

*

*/

#include "node.h"

#include <stdio.h>

#include <malloc.h>

#include <memory.h>

#include <assert.h>

/*

* Date: 2012-6-19

* Author: Ganyue

* Description: This function create the chain head.

*

*/

node *chain_create()

{

node *head = NULL;

head = malloc (sizeof(node));

memset(head,0,sizeof(node));

return head;

}

/*

* Date: 2012-6-19

* Author: Ganyue

* Description: This function helps you add node on the chain.

* Parameter: head The chain's head point.

* number The number you want add into the chain

* p[] The "words" you want to add into the chain.

*

*/

int chain_add(node *head,int number,char p[])

{

assert(head != NULL);

node *pnew = malloc(sizeof(node));

memset(pnew,0,sizeof(node));

pnew->number = number;

strcpy(pnew->words,p);

node *pt;

pt = head;

while(pt->pnext != NULL)

{

pt = pt->pnext;

}

pt->pnext = pnew;

pnew->pnext = NULL;

return 0;

}

/*

* Date: 2012-6-20

* Author: Ganyue

* Description: This function delete chain's node according to number.

* Parameter: head The chain's head point.

* number The number you pointed to delete.

*/

void chain_del(node *head,int number)

{

assert(head!=NULL);

node *pt = head;

while (pt->pnext != NULL)

{

if (pt->pnext->number == number)

{

node *pt1=NULL;

pt1 = pt->pnext;

pt->pnext = pt->pnext->pnext;

free(pt1);

}

else

{

pt = pt->pnext;

}

}

}

/*

* Date: 2012-6-20

* Author: Ganyue

* Description: This function delete chain's node according to number.

* Parameter: head The chain's head point.

* number The number you pointed to delete.

*/

void chain_insert(node *head,int location,int number,char words[])

{

assert(head!=NULL);

node *pi=NULL,*pt = head;

pi = malloc(sizeof(node));

memset(pi,0,sizeof(node));

pi->number = number;

strcpy(pi->words,words);

while (pt->pnext != NULL)

{

pt = pt->pnext;

if (pt->number == location)

{

node *pt1 = pt->pnext;

pt->pnext = pi;

pi->pnext = pt1;

break;

}

}

printf("No this location,it will not be inserted,the number in node are as follows:\n");

chain_output(head);

}

/*

* Date: 2012-6-19

* Author: Ganyue

* Description: This function helps you find the "words" in the chain.

* Parameter: head The chain's head point.

* words[] The words you want to find in the chain.

*/

int chain_find(node *head,char words[])

{

assert(head!=NULL);

int num_of_words = 0;;

node *pt = head;

while(pt->pnext != NULL)

{

pt = pt->pnext;

if (strcmp(pt->words,words) == 0)

{

num_of_words++;

}

}

return num_of_words;

}

/*

* Date: 2012-6-19

* Author: Ganyue

* Description: print the chain's contents.

* Parameter: head The chain's head point.

*

*/

void chain_output(node *head)

{

assert(head != NULL);

node *pt = head;

while(pt->pnext !=NULL)

{

pt = pt->pnext;

printf("%d銆? %s\n",pt->number,pt->words);

}

}

文件三:node_main.c

#include <stdio.h>

#include "node.h"

int main(int *argc,char* argv[])

{

node *head = NULL;

head = chain_create();

chain_add(head,1,"hello,word!");

chain_add(head,2,"hello,word!");

chain_add(head,3,"hello,word!");

chain_add(head,4,"hello,word!");

printf("--------------------------------------\n");

chain_output(head);

printf("--------------------------------------\n");

int num =chain_find(head,"hello,word!");

if (num == 0)

{

printf("Can't find!\n");

}

else

{

printf("Find this world in chain for %d times\n",num);

}

chain_del(head,3);

printf("--------------------------------------\n");

chain_output(head);

chain_insert(head,2,3,"I am inserted!");

printf("--------------------------------------\n");

chain_output(head);

chain_insert(head,6,3,"I am inserted!");

return 0;

}

文件四:Makefile

node.out:node.o node_main.o

gcc -o node.out node.o node_main.o

node.o:node.c node.h

gcc -c node.c

node_main.o:node_main.c node.h

gcc -c node_main.c

clean:

rm -rf node.out node.o node_main.o
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: