单链表数据存储结构(c语言实现)

1.list.h

# ifndef _LIST_H
# define _LIST_H

typedef struct _node
{
 void * data;
 struct _node * next;
}NODE;

typedef struct
{
 NODE * head;
 NODE * last;
 int length;
}LIST;

LIST * InitList();
int InsertList(LIST *l, void * data, int size);

 

NODE * FindNodeByKey(LIST *l, void * key, int (*compare)(void *, void *));
int DeleteListByKey(LIST * l, void * key, int (*compare)(void *, void *));

 

# endif

 

2.list.c

# include "list.h"
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

LIST * InitList()
{
 LIST * l = (LIST *)malloc(sizeof(LIST));
 if(NULL == l)
  exit(-1);
 memset(l, 0, sizeof(LIST));
 return l;
}

int InsertList(LIST *l, void * data, int size)
{
 NODE * n = NULL;
 if(NULL == l || data == NULL)
  return 0;
 n = (NODE *)malloc(sizeof(NODE));
 if(n == NULL)
  return 0;
// n->data = data;有bug存在
 n->data = malloc(size);
 if(NULL == n->data)
 {
  free(n);
  return 0;
 }
 memcpy(n->data, data, size);

 n->next = NULL;
 if(NULL == l->head)
  l->head = n;
 else
  l->last->next = n;
 l->last = n;
 l->length++;
 return 1;
}

 

NODE * FindNodeByKey(LIST *l, void * key, int (*compare)(void *, void *))
{
 NODE * p = NULL;
 
 if(l==NULL || key==NULL || compare==NULL)
 return 0;

 p = l->head;
 while(p)
 {
  if(compare(p->data, key) == 1)
   return p;
  p = p->next;
 }
 return NULL;
}

NODE * FindNodePre(LIST *l, void * key, int (*compare)(void *, void *), NODE ** pre)
{
 NODE * p = NULL;
 
 if(l==NULL || key==NULL || compare==NULL || pre == NULL)
 return 0;

 p = l->head;
 *pre = NULL;
 while(p)
 {
  if(compare(p->data, key) == 1)
   return p;
  * pre = p;
  p = p->next;
 }
 return NULL;
}

int DeleteListByKey(LIST * l, void * key, int (*compare)(void *, void *))
{
 NODE * p = NULL;
 NODE * q = NULL;
 
 p = FindNodePre(l, key, compare, &q);
 if(NULL == p)
  return 0;
 if(NULL == q)
 l->head = p->next;
 else
  q->next = p->next;
 if(p == l->last)
  l->last = q;
 free(p->data);
 free(p);
 l->length--;
 
 return 1;
}

 

3.main.c

# include "list.h"
# include <string.h>
# include <stdio.h>

struct STU
{
 char sno[5];
 char name[20];
 int age;
 int score;
}s[3] = {
 {"S01", "zhang san", 15, 90},
 {"S02", "li si", 13, 70},
 {"S03", "wang wu", 12, 50}
};

int CompareByName(void * info, void * key)
{
 struct STU * stu = (struct STU *) info;
 char * name = (char *)key;
 return strcmp(stu->name, name) == 0 ? 1 : 0;
}

int CompareBySno(void * info, void * key)
{
 struct STU * stu = (struct STU *) info;
 char * sno = (char *)key;
 return strcmp(stu->sno, sno) == 0 ? 1 : 0;
}

void main()
{
 int i;
 NODE * res = NULL;
 char name[] = "Li bai";
 char sno[] = "S02";
 LIST * list = InitList();
 for(i=0; i<3; ++i)
 {
  InsertList(list, &s[i], sizeof(s[i]));
 }
 res = FindNodeByKey(list, name, CompareByName);
 if(res == NULL)
  printf("The value is not the list!\n");
 else
  printf("The value is found!\n");

 if(DeleteListByKey(list, sno, CompareBySno))
  printf("Delete success\n");
 else
  printf("Delete fail\n");
 
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是单链表数据结构的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct Node { int data; // 存储节点的数据 struct Node* next; // 存储指向下一个节点的指针 }; // 在链表头部插入一个节点 void insertAtHead(struct Node** headRef, int newData) { // 创建新节点 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; // 新节点指向当前头节点 newNode->next = *headRef; // 更新头指针 *headRef = newNode; } // 在链表尾部插入一个节点 void insertAtTail(struct Node** headRef, int newData) { // 创建新节点 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = NULL; // 如果链表为空,则将新节点设置为头节点 if (*headRef == NULL) { *headRef = newNode; return; } // 遍历链表,找到最后一个节点 struct Node* lastNode = *headRef; while (lastNode->next != NULL) { lastNode = lastNode->next; } // 将新节点添加到最后一个节点后面 lastNode->next = newNode; } // 在链表中查找一个节点 struct Node* searchNode(struct Node* head, int searchData) { // 遍历链表,查找节点 while (head != NULL) { if (head->data == searchData) { return head; } head = head->next; } // 如果没有找到,则返回 NULL return NULL; } // 从链表中删除一个节点 void deleteNode(struct Node** headRef, int deleteData) { // 如果链表为空,则直接返回 if (*headRef == NULL) { return; } // 如果要删除的节点是头节点,则更新头指针 if ((*headRef)->data == deleteData) { *headRef = (*headRef)->next; return; } // 遍历链表,查找要删除的节点 struct Node* prevNode = *headRef; struct Node* currNode = (*headRef)->next; while (currNode != NULL) { if (currNode->data == deleteData) { // 将前一个节点的 next 指针指向当前节点的下一个节点 prevNode->next = currNode->next; // 释放当前节点的内存空间 free(currNode); return; } prevNode = currNode; currNode = currNode->next; } } // 打印链表 void printList(struct Node* head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { // 创建一个空链表 struct Node* head = NULL; // 在链表头部插入节点 insertAtHead(&head, 5); insertAtHead(&head, 4); insertAtHead(&head, 3); insertAtHead(&head, 2); insertAtHead(&head, 1); // 在链表尾部插入节点 insertAtTail(&head, 6); insertAtTail(&head, 7); insertAtTail(&head, 8); // 打印链表 printList(head); // 输出:1 2 3 4 5 6 7 8 // 查找节点 struct Node* searchResult = searchNode(head, 3); if (searchResult != NULL) { printf("Found: %d\n", searchResult->data); // 输出:Found: 3 } else { printf("Not found.\n"); } // 删除节点 deleteNode(&head, 2); // 打印链表 printList(head); // 输出:1 3 4 5 6 7 8 return 0; } ``` 以上代码实现单链表的基本操作,包括在链表头部插入节点、在链表尾部插入节点、查找节点、删除节点等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值