一个简单的C语言制作的单链表

C_LINK_LIST


  • 这是单链表和双链表的基本结果
点击查看代码
//single_link_list
struct NODE
 int value;
 struct NODE * next;
}
```c
//double linked list
struct NODE{
 int value;
 struct NODE * previous;
 struct NODE * next;
}
这是自己制作的一个C语言单链表,他的功能包括创建一个单链表,查找删除特定的值,并且实现倒序单链表。
她的不足之处在于他无法在主函数中特定停止,等待用户进行输入。
This is my code.
```c
#include <stdlib.h>
#include <stdio.h>
#include "singly_linked_list.h"
//struct NODE * sll_reverse(struct NODE * first);
int sll_remove(Node ** rootp, int value);
int dll_insert(Node ** rootp, int value);
Node * sll_reverse(Node *first);
int main(){
 int num;
 Node *first = (Node *)malloc(sizeof(Node));
 if(first == NULL){
 perror("NO Buffle: ");
 return 1;
 }
 first->value = 3;
 first->link = NULL;
 Node *rootp = first;
 while (scanf("%d" , &num) != EOF){
 dll_insert(&rootp, num);
 }
 Node * ptr = rootp;
 printf("\n");
 while(ptr != NULL){
 printf(" %d", ptr->value);
 ptr = ptr->link;
 }
 /* getchar();getchar();
 printf("Input a single number you want to remove: ");
 getchar();
 while(scanf("%d", &num) != EOF){
 sll_remove(&rootp, num);
 }
 ptr = rootp;
 while(ptr != NULL){
 printf(" %d", ptr->value);
 ptr = ptr->link;
 }*/
 rootp = sll_reverse(rootp);
 printf("\n");
 ptr = rootp;
 while(ptr != NULL){
 printf(" %d", ptr->value);
 ptr = ptr->link;
 }
 ptr = rootp;
 while(ptr != NULL){
 Node * temp=ptr;
 ptr = ptr->link;
 free(temp);
 }
 return 0;
}
int dll_insert(Node ** rootp, int value){
 Node *previous = NULL;
 Node *current = *rootp;
 while(current != NULL && current->value < value){
 previous = current;
 current = current->link;
 }
 if (current != NULL && current-> value == value)
 return 0;
 Node *new = (Node *)malloc(sizeof(Node));
 if (new == NULL){
 perror("NO buffle : ");
 exit(EXIT_FAILURE);
 }
 new->value = value;
 new->link = current;
 if(previous != NULL){
 previous->link = new;
 }
 else{
 *rootp = new;
 }
 return 0;
}
int sll_remove(Node ** rootp,int value){
 Node *previous = NULL;
 Node *current = *rootp;
 
 while(current != NULL && current->value != value){
 previous = current;
 current = current->link;
 }
 if(current != NULL){
 Node * temp = current;
 current = current->link;
 free(temp);
 if(previous != NULL){
 return 0;
 }
 else {
 *rootp = current;
 return 0;
 }
 }
 else {
 printf("No this module. ");
 return 1;
 }
}
 
Node * sll_reverse(Node * first){
 int i = 0;
 Node *temp = first;
 while(temp != NULL){
 temp = temp->link;
 i++;
 }
 if( i == 0){
 printf("None Node.");
 return first;
 }
 Node *array[i];
 temp = first;
 for(int j = 0; j< i;j++){
 ar#include <stdlib.h>
#include <stdio.h>
#include "singly_linked_list.h"
//struct NODE * sll_reverse(struct NODE * first);
int sll_remove(Node ** rootp, int value);
int dll_insert(Node ** rootp, int value);
Node * sll_reverse(Node *first);
int main(){
 int num;
 Node *first = (Node *)malloc(sizeof(Node));
 if(first == NULL){
 perror("NO Buffle: ");
 return 1;
 }
 first->value = 3;
 first->link = NULL;
 Node *rootp = first;
 while (scanf("%d" , &num) != EOF){
 dll_insert(&rootp, num);
 }
 Node * ptr = rootp;
 printf("\n");
 while(ptr != NULL){
 printf(" %d", ptr->value);
 ptr = ptr->link;
 }
 /* getchar();getchar();
 printf("Input a single number you want to remove: ");
 getchar();
 while(scanf("%d", &num) != EOF){
 sll_remove(&rootp, num);
 }
 ptr = rootp;
 while(ptr != NULL){
 printf(" %d", ptr->value);
 ptr = ptr->link;
 }*/
 rootp = sll_reverse(rootp);
 printf("\n");
 ptr = rootp;
 while(ptr != NULL){
 printf(" %d", ptr->value);
 ptr = ptr->link;
 }
 ptr = rootp;
 while(ptr != NULL){
 Node * temp=ptr;
 ptr = ptr->link;
 free(temp);
 }
 return 0;
}
int dll_insert(Node ** rootp, int value){
 Node *previous = NULL;
 Node *current = *rootp;
 while(current != NULL && current->value < value){
 previous = current;
 current = current->link;
 }
 if (current != NULL && current-> value == value)
 return 0;
 Node *new = (Node *)malloc(sizeof(Node));
 if (new == NULL){
 perror("NO buffle : ");
 exit(EXIT_FAILURE);
 }
 new->value = value;
 new->link = current;
 if(previous != NULL){
 previous->link = new;
 }
 else{
 *rootp = new;
 }
 return 0;
}
int sll_remove(Node ** rootp,int value){
 Node *previous = NULL;
 Node *current = *rootp;
 
 while(current != NULL && current->value != value){
 previous = current;
 current = current->link;
 }
 if(current != NULL){
 Node * temp = current;
 current = current->link;
 free(temp);
 if(previous != NULL){
 return 0;
 }
 else {
 *rootp = current;
 return 0;
 }
 }
 else {
 printf("No this module. ");
 return 1;
 }
}
 
Node * sll_reverse(Node * first){
 int i = 0;
 Node *temp = first;
 while(temp != NULL){
 temp = temp->link;
 i++;
 }
 if( i == 0){
 printf("None Node.");
 return first;
 }
 Node *array[i];
 temp = first;
 for(int j = 0; j< i;j++){
 array[j] = temp;
 temp = temp->link;
 }
 Node *new = array[i-1];
 for(int k = i-1;k >= 0;k--){
 temp = array[k];
 if(k != 0){
 temp->link = array[k-1];
 }
 else{
 temp->link = NULL;
 }
 }
 return new;
}
ray[j] = temp;
 temp = temp->link;
 }
 Node *new = array[i-1];
 for(int k = i-1;k >= 0;k--){
 temp = array[k];
 if(k != 0){
 temp->link = array[k-1];
 }
 else{
 temp->link = NULL;
 }
 }
 return new;
}

头文件: single_link_list.h

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

这是我制作的双链表
双链表与单链表的区别:单链表只能按某种顺序执行,而双链表在每个节点中都有前一个节点和后一个节点的指针,能够访问前一个节点和后一个节点。
这是我编写的双链表实现方式:

#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
 struct NODE * fwd;
 struct NODE * bwd;
 int value;
} Node;
Node * insert(Node *, int );
int main(){
 int new_value;
 Node * top;
 top = (Node *)malloc(sizeof(Node));
 if(top == NULL){
 perror("No memory: ");
 exit(EXIT_FAILURE);
 }
 top->bwd = NULL;
 top->fwd = NULL;
 printf("Hello linked list.\n");
 printf("Input some integet(-1 to quit). ");
 while((scanf("%d", &new_value) == 1) && new_value != -1){
 top=insert(top, new_value);
 }
 if(top->fwd == NULL){
 printf("NULL in top.\n");
 free(top);
 return 0;
 }
 for(Node * temp=top->fwd; temp != NULL; temp = temp->fwd){
 printf("%d ", temp->value);
 }
 //Node * current = top;
 Node * current = top->fwd;
 while(current != NULL){
 Node * temp = current;
 current = current->fwd;
 free(temp);
 }
 free(top);
 return 0;
}
Node * insert(Node * rootp, int value){
 Node * this;
 Node * next;
 Node * new;
 
 for (this = rootp; (next = this->fwd) != NULL ; this = next ){
 if(next->value == value)
 return rootp;
 if (this->value > value)
 break;
 }
 new = (Node *)malloc(sizeof(Node));
 if (new == NULL){
 perror("No memory: ");
 exit(EXIT_FAILURE);
 }
 new->value = value;
 if (next != NULL){
 new->fwd = next;
 if (this != rootp){
 this->fwd = new;
 new->bwd = this;
 }
 else{
 rootp->fwd = new;
 new->bwd = NULL;
 }
 next->bwd = new;
 }
 else{
 new->fwd = NULL;
 if(this != rootp){
 this->fwd = new;
 new->bwd = this;
 }
 else{
 rootp->fwd = new;
 new->bwd = NULL;
 }
 rootp->bwd = new;
 }
 return rootp;
}

2025-04-08 12:37:42 星期二

作者:RUST717原文地址:https://www.cnblogs.com/csheep-blog/p/18814387

%s 个评论

要回复文章请先登录注册