单链表的增、删、改、查(原创)
由 王宇 原创并发布:
以下代码均在linux 环境下,gcc v4.5 编译调试通过。
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }*head; void append(int number) { struct node *append_node = NULL, *right = NULL; append_node = (struct node *)malloc(sizeof(struct node)); if(append_node == NULL) { perror("Error: memory alloced failed at function of append!"); exit(EXIT_FAILURE); } append_node->data = number; append_node->next = NULL; if(head == NULL) { head = append_node; return; } right = head; while(right->next != NULL) { right = right->next; } right->next = append_node; } void add(int number) { struct node *add_node = NULL; add_node = (struct node *)malloc(sizeof(struct node)); if(add_node == NULL) { perror("Error: memory alloced failed at function of add!"); exit(EXIT_FAILURE); } add_node->data = number; add_node->next = NULL; if(head== NULL) { head = add_node; } else { add_node->next = head; head = add_node; } } void insert_at(int number, int position) { int i; struct node *insert_node = NULL, *left = NULL, *right = NULL; left = head; if(left == NULL) { perror("Error: linked list wasnot created!"); return; } if(position < 1) { perror("Error: Did not find position!"); return; } for(i = 0; i < position; i++) { if(left != NULL ) { if(i == position - 1) { right = left->next; break; } else { left = left->next; } } else { perror("Error: Did not find position!"); return; } } insert_node = (struct node *)malloc(sizeof(struct node)); if(insert_node == NULL) { perror("Error: memory alloced failed at function of append!"); exit(EXIT_FAILURE); } insert_node->data = number; insert_node->next = right; left->next = insert_node; } int delete_number(int number) { struct node *linked_list = NULL, *previous = NULL; linked_list = head; while (linked_list != NULL) { if(linked_list->data == number) { if(linked_list == head) { head = linked_list->next; free(linked_list); return 1; } else { previous->next = linked_list->next; free(linked_list); return 1; } } else { previous = linked_list; linked_list = linked_list->next; } } return 0; } void delete_all() { struct node *linked_list = NULL; linked_list = head; while ( head != NULL) { linked_list = head->next; head = head->next; free(linked_list); } } void display() { struct node *linked_list = NULL; linked_list = head; while ( linked_list != NULL) { printf("Data of linked list is %d \n", linked_list->data); linked_list = linked_list->next; } } int count_sum() { int sum = 0; struct node *linked_list = NULL; linked_list = head; while ( linked_list != NULL) { sum++; linked_list = linked_list->next; } return sum; } int main() { int i, number, position; while(1) { printf("\nList Operations\n"); printf("===============\n"); printf("1.Create\n"); printf("2.Add\n"); printf("3.Append\n"); printf("4.Insert\n"); printf("5.Display\n"); printf("6.Size\n"); printf("7.Delete\n"); printf("8.DeleteAll\n"); printf("0.Exit\n"); printf("Enter your choice : "); if(scanf("%d",&i) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { switch(i) { case 1: for(i = 1; i < 10; i++) { append(i); } break; case 2: printf("Enter the number to add: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { add(number); } break; case 3: printf("Enter the number to append: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { append(number); } break; case 4: printf("Enter the number to insert: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { printf("Enter the position: \n"); if(scanf("%d",&position) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { insert_at(number, position); } } break; case 5: display(); break; case 6: number = count_sum(); printf("Sum of linked list is %d", number); break; case 7: printf("Enter the number to delete: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { delete_number(number); } break; case 8: delete_all(); break; case 0: exit(1); break; default: printf("Invalid option\n"); } } } exit(1); }
相关推荐
Java实现单链表的增删改查以及选择、冒泡、反转排序等功能的实现!
学生信息管理系统c++(单链表增删改查)
VS2008运行环境 c语言实现单链表增删改查操作 并输出到控制台上
C语言编写的单链表增删改查,实现了单链表的增加,删除,修改,查询等功能
链表是一种常见的数据结构,资源包含了我对单链表的增删改查实现,本资源是不带头结点的单链表的增删改查,个人能力有限,希望指出不足之处,与大家共勉
在本篇文章里小编给大家整理了关于java实现单链表增删改查的实例内容,需要的朋友们可以参考下。
C语言简单的单链表增删改查。
针对单链表的建立以及赋予相关功能,实现信息的增删改查等,实验报告内容稍微欠缺,请自行修改
简易单链表增删改查功能实现。新增内容:新增单链表LruCache算法增删改查,对学习LruCache 算法有一定帮助。
单链表的增删改查,包含有序、无序、重复和不重复,给出了数据结构,以及增删改查的源代码
实现单链表的增删改查,,仅供参考,并不太完善
用C#实现单链表,使用图形界面,功能有增(头插,尾插),删, 改, 能自定义节点个数,还有倒置等功能,是学习链式存储是的练习程序,多多指教,jiehanwang@126.com
单链表源码,包括增删改查
链表去重 基于java的单链表增、删、改、查、去重操作
本库为C语言(Linux环境下编写的适用于所有C的数据结构库函数)。包括了最基础也是最常用的增删改查功能函数,队列,栈,链表(单链表,双链表,循环链表等都有),树的增删改查函数。程序可靠。
大三上学期数据结构第一次实验,内容: 1)数据文件为cet4word.txt,其中每行描述了一个词条,包含英文单词、词性及中文释义等属性 2)将每个词条定义为一个结构体 3)能够实现词典的装载、添加、删除、按英文单词...
链表是由一连串节点组成的数据结构,每个节点包含一个数据值和一个指向下一个节点的指针。链表可以在头部和尾部插入和删除节点,因此可以在任何地方插入和删除节点,从而使其变得灵活和易于实现。...
23564654564564554665我沁人肺腑大时代
单链表基本操作 基于C的单链表的创建,以及对单链表的增、删、查、改、和打印等基本操作