数据结构—单链表

数据结构—单链表

   数据结构的单链表终于要搞完了,冲冲冲!
   在编写的时候发现有些功能重复了,这子不太好!到时候精简吧!

//
//  slist.h
//  slist
//
//  Created by 小刘 on 2020/11/21.
//  Copyright © 小刘 All rights reserved.
//

#ifndef __SLITS__H_
#define __SLITS__H_


#include 
#include 
#include 
#include 

#define ElemType int

#define DEBUG_MSG(x) printf ("[DEBUG][%s][%d]:",__FUNCTION__,__LINE__); printf(x);
#define INFO_MSG(x)  printf ("[INFO.][%s][%d]:",__FUNCTION__,__LINE__); printf(x);
#define ERROR_MSG(x) printf ("[ERROR][%s][%d]:",__FUNCTION__,__LINE__); printf(x);

#define FUNC_OK         0
#define FUNC_ERR        -1

#define MALLOC(type,num) (type*)malloc(sizeof(type) * (num));

typedef struct Node {
     
    ElemType data;
    struct Node *next;
}Node;

typedef Node *ListNode;


void InitTheListHead(ListNode *head);//初始化头节点
void traverseTheList(ListNode head);//打印

void InsertTheListHead(ListNode *head);//前插法1
void InsertTheListTail(ListNode *head);//尾插法
void InsertTheListHeadway1(ListNode *head); //前插法2
void InvertedTheList(ListNode *head);//逆转

int LinkedListNumberLookup(ListNode *head, ElemType num);//数字查找
int LinkedListSubscriptLookup(ListNode *head,ElemType sub);//下标查找

int DeletesElementsLinkedList(ListNode *head, ElemType sub,ElemType num); // 删除
int InsertLinkedListMiddle(ListNode *head, ElemType sub,ElemType num); // 插入
int DestoryLinkedList(ListNode *head); //损毁链表
int MergeLinkedList(ListNode *La , ListNode *Lb ,ListNode *Lc); //合并


#endif
//
//  slist.c
//  slist
//
//  Created by 小刘 on 2020/11/21.
//  Copyright © 小刘 All rights reserved.
//

#include "slist.h"

void InitTheListHead(ListNode *head){
     
    * head = (Node *)malloc(sizeof(Node));
    assert(*head !=NULL);
    (*head)->next = NULL;
}


void InsertTheListHeadway1(ListNode *head){
     
    int num;
    while(scanf("%d",&num),num!=-1){
     
        Node * tail = (Node *)malloc (sizeof(Node));
        assert(tail !=NULL);
        tail->data =num;
        tail->next = (*head)->next;
        (*head)->next = tail ;
    } 
}//

void InsertTheListHead(ListNode *head){
     

    int num;
    Node *l1;
    while(scanf("%d",&num),num!=-1){
     
        l1 = (Node *) malloc (sizeof(Node));
        assert(l1!=NULL);
        l1->data = num;
        l1->next = NULL;
        l1->next = (*head)->next;
       (*head)->next = l1;
     }
}

//尾插法
void InsertTheListTail(ListNode *head){
     
    int num ;
        Node *l2 = NULL;
        Node *l3 = NULL;
       (*head)->next = NULL;
       l3 = (*head);
    while(scanf("%d",&num),num!=-1){
       
        l2 = (Node*)malloc (sizeof(Node));
        assert(l2 != NULL);
        l2->data = num;
        l3->next = l2;
        l3 = l2;
    }
    l3->next =  NULL;
}


void traverseTheList(ListNode head){
      //遍历

    Node *p = head->next; 
    while (p != NULL){
     
        printf(" %d ",p->data);
        p = p->next; 
    }
}


int LinkedListNumberLookup(ListNode *head, ElemType num){
     //数字查找

    int i = 1 ;
    Node *p ;
    p = (*head)->next;
    while(p->data != num){
     
        p = p->next;
        i++;
    }
    return i;
}

int LinkedListSubscriptLookup(ListNode *head,ElemType sub){
     //下标查找

    int i=1;
    for( Node *p = (*head)->next; p != NULL; p = p->next ){
       
            if (i == sub){
     
                return p->data;
        }
    i++;
    }
    return 1;
}


int InsertLinkedListMiddle(ListNode *head, ElemType sub,ElemType num){
      
        int j = 0;
        if (LinkedListSubscriptLookup(head,sub)==1){
     
                printf("没有找到元素的位置\n");
                return EXIT_FAILURE;
        }
        Node * p =(*head);
        while(p && j<sub-1){
     
            p = p->next;
            j++;
        }
        if(!p || j>sub-1){
     
            return EXIT_FAILURE;
        }
        Node * s = (Node*)malloc(sizeof(Node));
        s->data = num;
        s->next = NULL;
        s->next = p->next; //等于号不能写反,
        p->next = s;
        
    return FUNC_OK;        
}


int DeletesElementsLinkedList(ListNode *head, ElemType sub,ElemType num){
     
    Node *p = (*head);
    int j =0;
    while(p->next && j<sub-1){
     
        p = p->next;
        j++;
    }
    if(!(p->next)||j > sub-1){
      
        printf("删除位置不合理");
        return 1;
    }
    Node *q;
    q =p->next;
    p->next = q->next;
    num = q->data;
    free(q);
    return FUNC_OK;
}


int DestoryLinkedList(ListNode *head){
     
    free(head);
}


int MergeLinkedList(ListNode *La , ListNode *Lb ,ListNode *Lc){
      // 合并
        Node *pa = (*La)->next;
        Node *pb = (*Lb)->next;
        Node *pc;
        (*Lc) = (*La) = pc; 
        while(pa&&pb){
     
            if(pa->data<=pb->data){
     
                pc->next  = pa;
                pc = pa;
                pa =pa->next;               
        }
        else{
     
            pc->next = pb;
            pc=pb;
            pb = pb->next;
            }
        }
    pc->next = pa?pa :pb;
    free(Lb);
    return FUNC_OK;
}


//如果把链表里面的值改变,那么应该就是改数据域就可以了

   这里面还是有些工作没有完成的比如说排序算法这些东东。在以前的写代码过程,一般都是搬运代码,如何更改代码,虽然这样开发一个项目可能快,但是对之间的编写没有多大的提升,就很垃圾,哈哈哈哈!我何时才能变成那个华为的老师一样强啊,python虽然好玩,但相比于C还是差了点意思。

你可能感兴趣的:(C&C++,数据结构,算法,链表)