链表插入算法

最近再看swish-e的源代码的时候,觉得里面的一种链表插入(新节点插入到末尾)的算法不错。
基本的算法中需要遍历一下(当然也可以增加一个tail节点指针)。我整理了一下,写了一个测试程序。
#include <stdio.h>
#include <unistd.h>
void
insertlist(struct conlist **list, char *line);
typedef union
{
struct conlist *listp;
char *data;
} otherlist;
struct conlist
{
char line[1];
struct conlist *next;
otherlist other_list;
};
int
main(int argc, char *argv[]) {
int opt;
struct conlist *list = NULL;
while((opt = getopt(argc, argv, "c:")) != -1) {
switch(opt) {
case 'c':{
while(*optarg) {
printf("the option is:%s/n",optarg);
optarg++;
}
}
break;
}
}
for(; optind < argc; optind++) {

//printf("argument: %s/n", argv[optind]);
insertlist(&list,argv[optind]);
}
struct conlist *startlist;
startlist = list;
if (startlist == NULL) {
printf(" list error/n");
}
while(startlist) {
printf("the content is: %s/n", startlist->line);
startlist = startlist->next;
}
return 0;
}
void
insertlist(struct conlist **listnode, char *line) {
struct conlist *plist, *newnode;
plist = *listnode;
newnode = (struct conlist *)malloc(sizeof(struct conlist) + strlen(line));
if (newnode == NULL) {
printf("malloc error/n");
}
newnode->next = NULL;
memcpy(newnode->line, line, strlen(line)+1);
printf("the string is %s:/n", newnode->line);
if (*listnode == NULL) {
*listnode = newnode;
printf("NULL /n");
}
else
{
printf("insert another/n");
plist->other_list.listp->next = newnode;
}
(*listnode)->other_list.listp = newnode;
//struct conlist *startlist;
//startlist = plist;
//while(startlist) {
// printf("the content is: %s/n", startlist->line);
// startlist = startlist->next;
//}
}


就是通过两个指针进行链表的插入,otherlist.listp也指向tail节点。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环双链表插入算法的C语言实现如下: ```c #include <stdio.h> #include <stdlib.h> // 定义双向链表节点结构体 typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; // 定义循环双向链表结构体 typedef struct List { Node* head; Node* tail; } List; // 初始化循环双向链表 void initList(List* list) { list->head = NULL; list->tail = NULL; } // 创建新节点 Node* createNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->prev = NULL; node->next = NULL; return node; } // 在链表尾部插入节点 void insertAtTail(List* list, int data) { Node* node = createNode(data); if (list->head == NULL) { list->head = node; list->tail = node; node->prev = node; node->next = node; } else { node->prev = list->tail; node->next = list->head; list->tail->next = node; list->head->prev = node; list->tail = node; } } // 在链表中间插入节点 void insertAtMiddle(List* list, int data, int position) { Node* node = createNode(data); Node* current = list->head; int i = 1; while (i < position && current != NULL) { current = current->next; i++; } if (current == NULL) { printf("Invalid position\n"); return; } node->prev = current->prev; node->next = current; current->prev->next = node; current->prev = node; } // 打印链表 void printList(List* list) { Node* current = list->head; if (current == NULL) { printf("List is empty\n"); return; } do { printf("%d ", current->data); current = current->next; } while (current != list->head); printf("\n"); } int main() { List list; initList(&list); insertAtTail(&list, 1); insertAtTail(&list, 2); insertAtTail(&list, 3); insertAtTail(&list, 4); insertAtMiddle(&list, 5, 3); printList(&list); return ; } ``` 该算法实现了循环双向链表的初始化、在链表尾部插入节点、在链表中间插入节点和打印链表等功能。其中,insertAtMiddle函数实现了在链表中间插入节点的功能,需要传入链表、要插入的数据和插入的位置三个参数。如果插入位置不合法,会输出"Invalid position"。最后,通过调用printList函数打印链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值