C 链表学习

      一个小小的链表demo,有助于初学者学习使用。

 [root@centos6 list]# ll
total 20
-rw-r--r-- 1 xuekun 500   12 Apr 22 05:44 f1.txt
-rw-r--r-- 1 xuekun 500 2970 Apr 22 06:46 linker.c
-rw-r--r-- 1 xuekun 500  497 Apr 22 06:26 linker.h
-rw-r--r-- 1 xuekun 500 1700 Jun  8 11:09 main.c
-rw-r--r-- 1 xuekun 500  114 Apr 22 03:07 Makefile

main.c文件:

[root@centos6 list]# cat main.c
/*
 * create by xk
 * update 2013-04-22
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "linker.h"
int main(void)
{
        struct link *chain;
        char c;
        int num=0;
        printf("#-----------------------#\n");
        printf("#    Created by xk      #\n");
        printf("#    Date 2012-08-19    #\n");
        printf("#-----------------------#\n");
        chain=list_create();
        while(1)
        {
                printf("#-----------------------#\n");
                printf("#   I:add student       #\n"); 
                printf("#   P:print list        #\n");
                printf("#   S:search student    #\n");
                printf("#   D:delete student    #\n");
                printf("#   W:write list file   #\n");
                printf("#   L:Load list file    #\n");
                printf("#   E:exit list         #\n");
                printf("#-----------------------#\n");
                printf("Please input your choice:");
                scanf("%s",&c);
                switch(c)
                {
                        case 'I':
                        case 'i':
                                list_insert(chain);
                                printf("add success!\n");
                                break;
                        case 'P':
                        case 'p':
                                printf("print:\n");
                                list_travel(chain);
                                break;
                        case 'S':
                        case 's':
                                printf("Input num:\n");
                                scanf("%d",&num);
                                struct node *s;
                                s=(struct node *)malloc( sizeof(struct node) );
                                if(s==NULL)
                                {
                                        printf("Create node fail!\n");
                                        exit(0);
                                }
                                s=search_list(chain, num);
                                printf("num:%d\n",s->num);
                                printf("score:%d\n",s->score);
                                break;
                        case 'D':
                        case 'd':
                                printf("Input num:\n");
                                scanf("%d",&num);
                                delete_list(chain, num);
                                printf("%dnode delete success!\n",num);
                                break;
                        case 'W':
                        case 'w':
                                save_list(chain);
                                break;
                        case 'L':
                        case 'l':
                                load_student(chain);
                                break;
                        case 'E':
                        case 'e':
                                free_list(chain);
                                exit(0);
                }
        }
        return 0;
}

 

linker.h文件:

[root@centos6 list]# cat linker.h
#ifndef _LINKER_H
#define _LINKER_H
typedef struct node {
        int num;
        int score;
        struct node *next;
}student;
struct link {
        int count;
        student *head;
        student *tail;

};

struct link *list_create();
int list_insert(struct link *chain);
void list_travel(struct link *chain);
struct node *search_list(struct link *chain,int num);
void delete_list(struct link *chain,int num);
void free_list(struct link *chain);
void save_list(struct link * chain);
void load_student(struct link * chain);

#endif

linker.c文件:

[root@centos6 list]# cat linker.c
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "linker.h"
struct link *list_create()
{
        struct link * linker;
        linker=(struct link*)malloc(sizeof (struct link) );
        assert(linker);

        linker->count=0;
        linker->head=NULL;
        linker->tail=NULL;
        return linker;
}
int list_insert(struct link *chain)
{
        student *q,*r;
        struct node *p;
        int num;
        int score;

        p=(struct node *)malloc( sizeof(struct node) );
        if(p==NULL)
        {
                printf("Create node fail!\n");
                return 1;
        }

        printf("num:\n");
        scanf("%d",&num);
        printf("score:\n");
        scanf("%d",&score);
        p->num=num;
        p->score=score;

        if(chain->count==0)
        {
                p->next=NULL;
                chain->count++;
                chain->head=p;
                chain->tail=p;

        }else
        {


                if(p->score <= chain->head->score )
                {
                        p->next=chain->head;
                        chain->head=p;
                        chain->count++;
                }
                r=chain->head;
                q=chain->head->next;
                while(q!=NULL)
                {
                        if(q->score < p->score)
                        {
                                r=q;
                                q=q->next;
                        }
                        else
                                break;

                }
                r->next=p;
                p->next=q;
                chain->count++;

        }
        return 0;

}
void list_travel(struct link *chain)
{
        student *tmpprint = NULL;

        assert(chain);
        tmpprint=chain->head;

        while(tmpprint)
        {

                printf("num=[%d]\t\tscore=[%d]\n",tmpprint->num,tmpprint->score);
                tmpprint=tmpprint->next;
        }

}
struct node *search_list(struct link *chain,int num)
{
        struct node *s;
        if(chain->count==0)
                printf("Chain is empty!\n");
        while(1)
        {
                if(chain->head->num==num)
                {
                        s=chain->head;
                        break;
                }else
                {
                        chain->head=chain->head->next;
                }
                continue;
        }
        return s;

}

void delete_list(struct link *chain, int num)
{
        student *p1,*p2;
        p1=chain->head;
        if(chain->count==0)
        {
                printf("\nlist null!");
        }
        while(num!=p1->num && p1->next!=NULL)
        {
                p2=p1;
                p1=p1->next;
        }
        if(num==p1->num)
        {
                if(p1==chain->head)
                        chain->head=p1->next;
                else
                        p2->next=p1->next;
                free(p1);
                printf("delete:%d\n",num);
                chain->count--;
        }
        else
                printf("%d not been found\n",num);

}
void free_list(struct link *chain)
{
        struct node *tmphead=chain->head;

        while(tmphead!=NULL)
        {
                chain->head=chain->head->next;
                free(tmphead);
                tmphead=chain->head;

        }
        printf("exit success!\n");
}
void save_list(struct link *chain)
{
                FILE *fp;
                char ch='1';
                struct node *p1;
                if((fp=fopen("f1.txt","w"))==NULL){
                        printf("File open error!\n");
                        exit(0);
                }
                fputc(ch,fp);
                for(p1=chain->head;p1;p1=p1->next){
                        fprintf(fp,"%d %d\n",p1->num,p1->score);
                }
                fclose(fp);
}
void load_student(struct link * chain)
{
        struct node *p1;
        FILE *fp;
        char ch;

        if((fp=fopen("f1.txt","r"))==NULL){
                printf("File open error!\n");
                exit(0);
        }

        ch=fgetc(fp);
        if(ch=='1'){

                while(!feof(fp)){
                        p1=(struct node *)malloc(sizeof(struct node));
                        fscanf(fp,"%d%d\n",&p1->num, &p1->score);
                        if(chain->count==0)
                        {
                                chain->head=p1;
                            chain->count++;
                        }
                        else
                        {
                                chain->tail->next=p1;
                            chain->count++;
                        }
                        chain->tail=p1;
                }
                chain->tail->next=NULL;
                fclose(fp);
        }
}

Makefile文件:

[root@centos6 list]# cat Makefile
CFLAGS= -Wall -g
all:main
main: main.o linker.o
        gcc -g $(CFLAGS) $^ -o $@
.PHONY: clean
clean:
        rm *.o main -f

 

转载于:https://my.oschina.net/u/1034011/blog/138056

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值