单链表 建立, 查找, 删除, 插入 操作

完善一下单链表操作.


linklist.c


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

#define     DEBUG_PRT(fmt,arg...)    printf(fmt,##arg)

typedef struct linklist
{
    int value;
    struct linklist *next;
}Mylist_s;

Mylist_s *create_list(int number)
{
    Mylist_s *head = NULL;
    Mylist_s *p = NULL;
    Mylist_s *s= NULL;
    int i = 0;
    
    if(NULL == (head = (Mylist_s *)malloc(sizeof(Mylist_s))))
    {
        DEBUG_PRT("malloc error \n");
        exit(1);
    }
    head->value = -1;
    head->next = NULL;
    
    p = head;
    for(i=0; i<number; i++)
    {
        if(NULL == (s = (Mylist_s *)malloc(sizeof(Mylist_s))))
        {
            DEBUG_PRT("malloc error \n");
            exit(1);
        }
        s->value = i;
        s->next = NULL;
        p->next = s;
        p = s;
    }

    return head;
}

void print_list(Mylist_s *head)
{
    Mylist_s *p = NULL;
    p = head->next;
    while(p != NULL)
    {
        DEBUG_PRT("%d ", p->value);
        p = p->next;
    }
    DEBUG_PRT("\n");
}

/*
* search s, return s
*   s -> r
*/
Mylist_s *search_list(Mylist_s *head, int value)
{
    Mylist_s *p = NULL;
    p = head->next;
    while(p != NULL)
    {
        if(value == p->value)
        {
            return p;
        }
        p = p->next;
    }

    return NULL;
}

/*
* search s, return t
*   t -> s -> r
*/
Mylist_s *search_pre_node(Mylist_s *head, int value)
{
    Mylist_s *p = NULL;
    Mylist_s *pre = NULL;
    p = head->next;
    pre = p;

    while(p != NULL)
    {
        if(value == p->value)
        {
            return pre;
        }
        pre = p;
        p = p->next;
    }

    return NULL;
}

/*
* del s
*   t -> s -> r
* =>t -> r
*/
void del_node(Mylist_s *t)
{
    Mylist_s *p = NULL;
    p = t->next;
    t->next = p->next;
    free(p);
}

/*
* insert s follow t
*   t -> r
* =>t -> s -> r
*/
void insert_node(Mylist_s *s, Mylist_s *t)
{
    s->next = t->next;
    t->next = s;
}

void main(void)
{
    Mylist_s *head = NULL;
    Mylist_s *pre = NULL;
    Mylist_s *new_node = NULL;
    
    head = create_list(10);
    DEBUG_PRT("Init list: \n");
    print_list(head);

    if(NULL == search_list(head, 7))
    {
        DEBUG_PRT("no such value \n");
        exit(1);
    }
    DEBUG_PRT("search_list() ok \n");
    
    if(NULL == (pre = search_pre_node(head, 7)))
    {
        DEBUG_PRT("no such value \n");     
        exit(1);
    }
    DEBUG_PRT("search_pre_node() ok \n");
    del_node(pre);
    DEBUG_PRT("After del 7, list: \n");
    print_list(head);
    
    if(NULL == (new_node = (Mylist_s *)malloc(sizeof(Mylist_s))))
    {
        DEBUG_PRT("can not malloc new_node \n");
        exit(1);
    }
    new_node->value = 100;
    new_node->next = NULL;
    insert_node(new_node, pre);
    DEBUG_PRT("After insert 100 follow 6, list: \n");
    print_list(head);
}


Makefile:


 BIN = /usr/bin/
 GCC = $(BIN)gcc
 CFLAG =
 INC = -I.
 LIB = -L.
 SRC = linklist.c
 TAG = linklist
 RM = /bin/rm
 CP = /bin/cp
 
 all:
     $(GCC) $(CFLAG) $(INC) $(LIB) $(SRC) -o $(TAG)
 clean:
     $(RM) -rf *.o $(TAG)


运行结果:

./linklist
Init list:
0 1 2 3 4 5 6 7 8 9
search_list() ok
search_pre_node() ok
After del 7, list:
0 1 2 3 4 5 6 8 9
After insert 100 follow 6, list:
0 1 2 3 4 5 6 100 8 9


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个用C++编写的单链表的例子,包括建立查找插入删除操作的实现: ```cpp #include <iostream> // 定义单链表节点结构 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 建立单链表 ListNode* createLinkedList(std::initializer_list<int> values) { ListNode* head = NULL; ListNode* tail = NULL; for (int val : values) { ListNode* newNode = new ListNode(val); if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } return head; } // 查找链表中是否存在某个值 bool searchLinkedList(ListNode* head, int target) { ListNode* curr = head; while (curr != NULL) { if (curr->val == target) { return true; } curr = curr->next; } return false; } // 在链表的指定位置插入节点 void insertNode(ListNode*& head, int pos, int val) { ListNode* newNode = new ListNode(val); if (pos == 0) { newNode->next = head; head = newNode; } else { ListNode* curr = head; int count = 0; while (curr != NULL && count < pos - 1) { curr = curr->next; count++; } if (curr != NULL) { newNode->next = curr->next; curr->next = newNode; } } } // 删除链表中指定位置的节点 void deleteNode(ListNode*& head, int pos) { if (pos == 0) { ListNode* temp = head; head = head->next; delete temp; } else { ListNode* curr = head; int count = 0; while (curr != NULL && count < pos - 1) { curr = curr->next; count++; } if (curr != NULL && curr->next != NULL) { ListNode* temp = curr->next; curr->next = curr->next->next; delete temp; } } } // 打印链表 void printLinkedList(ListNode* head) { ListNode* curr = head; while (curr != NULL) { std::cout << curr->val << " "; curr = curr->next; } std::cout << std::endl; } int main() { // 创建链表 1 -> 2 -> 3 -> 4 -> 5 ListNode* head = createLinkedList({1, 2, 3, 4, 5}); printLinkedList(head); // 查找链表中是否存在值为 3 的节点 bool found = searchLinkedList(head, 3); std::cout << "Found 3 in the linked list: " << (found ? "true" : "false") << std::endl; // 在位置 2 插入值为 6 的节点 insertNode(head, 2, 6); printLinkedList(head); // 删除位置 4 的节点 deleteNode(head, 4); printLinkedList(head); return 0; } ``` 这段代码实现了单链表建立查找插入删除操作。你可以根据需要进行修改和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值