自己编的C语言单链表的实现

看了下C primer plus的最后一章以及c和指针中讲解链表的部分,就动手写了个链表的功能实现程序,用纯C编写,在vc环境下,因此文件后缀为cpp。

头文件sll_node.h

#include<stdio.h>
#include<stdlib.h>

struct NODE
{
   int value;
   struct NODE *next;
};
typedef struct NODE Node;

//这里的root是在link_main文件中的定义的,同时应用于input.cpp文件,

//该文件中没有定义指针root,
//其结果是在调用过input函数之后,root值改变,不再为NULL;

extern Node *root;

bool sell_insert(Node **,int );  //插入节点函数

void show(Node *);    //显示链表数据函数

void input_data(Node **);  //输入链表数据函数

void free_list(Node **);  //释放链表内存函数

unsigned int count(Node *);     //链表节点计数函数

bool check(Node *,int);   //检查输入的数据是否在聊表中已经存在的函数

//链表排序函数——冒泡法,不改变节点位置,只改变节点中数据的存储位置

void sort(Node *);  

void delete_data(Node **,int); //删除节点函数


 

子函数文件node.c

#include "sll_node.h"

//遍历链表——检查输入的数据是否与链表中的数据重复
bool
check(Node *root,int value)

{
   Node *current;
   current = root;
   while(current != NULL)
   {
     if(current->value != value)
        current = current->next;
    else
        break;
   }

   if(current == NULL)
      return true;       //输入的数据没有与链表中的数据重复,返回true
   else
     return false;
}

 

//删除节点函数


void
delete_data(Node **phead,int delete_value)
{
   Node *current;
   Node *previous;
   Node *next;
   Node *pt;

   current = *phead;
   previous = NULL;

   while(current->value != delete_value)
   {
      previous = current;
      current = current->next;
      next = current->next;
   }

   if(previous == NULL)
      *phead = current->next;
    else
      previous->next = next;  
   pt = current;
   free(pt);
}

 

//输入函数
void
input_data(Node **phead)
{
    int value;
    Node *current,*prev;
    root = *phead;
    puts("Enter the first value(q to end):");
    while(scanf("%d",&value)==1)
    {
       while(getchar() != '\n')
           continue;

       current = (Node *)malloc(sizeof(Node));
       if(root == NULL)
           root = current;
       else
           prev->next = current;
       current->next = NULL;
       current->value = value;
       puts("Enter the next value(q to end):");
       prev = current;
    }
}


 

//插入节点函数
bool
sell_insert(Node **phead,int new_value)
{
    Node *current;
    Node *previous;
    Node *newn;
 
    current = *phead;
    previous = NULL;

    while(current != NULL && current->value < new_value)
    {
       previous = current;
       current = current->next;
    }

    newn = (Node *)malloc(sizeof(Node));
    if(newn == NULL)
        return false;

    newn->value = new_value;
    newn->next = current;
    if(previous==NULL)
        *phead = newn;
    else
        previous->next = newn;

    return true;
}

 


//节点计数函数
unsigned int
count(Node *root)
{
    unsigned int count = 0;
    while(root != NULL)
    {
        ++count;
        root = root->next;
    }
 
    return count;
}

 

//显示函数
void
show(Node *root)
{
    Node *current;   
    if(root == NULL)
    printf("No data entered.");

    current = root;
    while(current != NULL)
    {
        printf(" %d ",current->value);
        current = current->next;
    }
}

 

//冒泡法排序链表函数——不改变节点位置,只改变节点中数据的存储位置void
sort(Node *root)
{
    Node *current;
    Node *s;
 
    for(current = root;current->next != NULL;current = current->next)
    {
       for(s = root;s->next != NULL;s = s->next)
       {  
           if(s->value > s->next->value)
           {
               int temp;
               temp = s->value;
               s->value = s->next->value;
               s->next->value = temp;
           }
       }
   }

}

 

//释放节点内存函数
void
free_list(Node **phead)
{
    Node *psave;
    while(*phead != NULL)
    {
        psave = (*phead)->next;
        free(*phead);
        *phead = psave;
    }
}

 

主函数实现文件link_main.c

#include "sll_node.h"

Node *root = NULL;

int main()
{

//输入数据,分配内存,并输入原始序列和排序后的序列
    input_data(&root);
    if(root== NULL)
        printf("No data entered.");
    else
    {
        printf("Here is the list:\n");
        show(root);
        printf("\nThe sorted list is:\n");
        sort(root);
        show(root);
        printf("\nTotal itms of the list is:\n %d",count(root));
    }


//输入需要添加的数据
    while(getchar() != '\n')
        continue; 

//这两句不可少,防止scanf因最后一次读取到的是字母而永远不会越过它
    int new_value;
    printf("\nNow enter the new value:\n");
    scanf("%d",&new_value);
    while(getchar() != '\n')
        continue;


//检查待添加的数据是否在链表中存在,若存在,提示重新输入,直到不存在为止
    while(!check(root,new_value) )
    {
        printf("Existed in the list ,please input another data:");
        scanf("%d",&new_value);
        while(getchar() != '\n')
           continue;
 }


//将待添加的数据加入新建的节点,并分配内存,插入链表,显示插入后的序列
    if(sell_insert(&root,new_value))
    {
        printf("\nNow the list is:\n");
        show(root);
        printf("\nNow total itms of the list is:\n %d",count(root));
    }
    else
        printf("insert error\n");
    printf("\n");


//输入待删除的数据
    int delete_value;
    printf("\nNow enter the deleted value:\n");
    scanf("%d",&delete_value);
    while(getchar() != '\n')
        continue;


 //如果待删除的数据不在链表中,提示重新输入,知道存在为止
    while(check(root,delete_value) )
    {
        printf("Not exist in the list ,please input a data in the list:");
        scanf("%d",&delete_value);
        while(getchar() != '\n')
            continue;
    }


//将待删除的数据所在的节点所占用的内存释放掉,即删除掉,重新链接链表,并显示删除数据后的序列

    delete_data(&root,delete_value);
    printf("\nNow the list is:\n");
    show(root);
    printf("\nNow total itms of the list is:\n %d",count(root));
    printf("\n");


 //释放链表所占用的内存

   free_list(&root);

   return 0;
}

运行结果:

 

 

关注我的新浪博客:http://blog.sina.com.cn/u/1921993171

关注我的新浪微博:http://weibo.com/u/1921993171?wvr=5&wvr=5&lf=reg

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,由于时间和篇幅限制,我无法提供完整的图书管理系统代码。但是,我可以提供一些单链表实现的示例代码,可以作为参考。 首先,定义一个图书结构体: ```c typedef struct book { int id; // 图书号 char name[50]; // 图书名称 char author[50]; // 作者 int quantity; // 库存数量 float price; // 单价 struct book *next; // 下一本图书 } Book; ``` 然后,定义一个单链表结构体: ```c typedef struct { Book *head; // 链表头 int count; // 链表长度 } BookList; ``` 接下来,实现一些基本的链表操作函数。 1. 创建链表 ```c BookList* createList() { BookList *list = (BookList*)malloc(sizeof(BookList)); list->head = NULL; list->count = 0; return list; } ``` 2. 添加图书到链表末尾 ```c void addBook(BookList *list, Book *book) { if (list == NULL || book == NULL) { return; } if (list->head == NULL) { list->head = book; } else { Book *p = list->head; while (p->next != NULL) { p = p->next; } p->next = book; } list->count++; } ``` 3. 从链表中删除图书 ```c void deleteBook(BookList *list, int id) { if (list == NULL || list->head == NULL) { return; } Book *p = list->head; Book *prev = NULL; while (p != NULL) { if (p->id == id) { if (prev == NULL) { list->head = p->next; } else { prev->next = p->next; } free(p); list->count--; break; } prev = p; p = p->next; } } ``` 4. 更新图书信息 ```c void updateBook(BookList *list, int id, char *name, char *author, int quantity, float price) { if (list == NULL || list->head == NULL) { return; } Book *p = list->head; while (p != NULL) { if (p->id == id) { strcpy(p->name, name); strcpy(p->author, author); p->quantity = quantity; p->price = price; break; } p = p->next; } } ``` 5. 查找图书 ```c Book* findBook(BookList *list, int id) { if (list == NULL || list->head == NULL) { return NULL; } Book *p = list->head; while (p != NULL) { if (p->id == id) { return p; } p = p->next; } return NULL; } ``` 6. 遍历链表 ```c void traverseList(BookList *list) { if (list == NULL || list->head == NULL) { return; } Book *p = list->head; while (p != NULL) { printf("ID: %d, Name: %s, Author: %s, Quantity: %d, Price: %.2f\n", p->id, p->name, p->author, p->quantity, p->price); p = p->next; } } ``` 以上是单链表实现的一些基本操作函数,可以根据需要进行扩展和修改,以实现图书管理系统的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值