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

文件一: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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值