其他分享
首页 > 其他分享> > 链表的增删改查

链表的增删改查

作者:互联网

#include<stdio.h>
#include<math.h>

//定义一个结点数据结构
typedef struct Node{
    int data; //数据域
    struct Node* pNext; //指针域
}NODE, *PNODE;

//NODE => struct Node
//PNODE => struct Node *

//函数声明
PNODE create_list();
void traverse_list(PNODE pHead);
bool is_empty(PNODE pHead);
int length_list(PNODE);
bool delete_list(PNODE ,int);
bool insert_list(PNODE ,int, int );
void sort_list(PNODE,int);
void swap(int* a,int * b);

int main(){

    PNODE pHead = NULL; //头指针为空  
    pHead = create_list();
    int len = length_list(pHead);
    traverse_list(pHead);
    sort_list(pHead,len);
    traverse_list(pHead);
    int val1,pos1;
    printf("插入的位置:");
    scanf("%d",&pos1);
    printf("插入的值为:");
    scanf("%d",&val1);
    insert_list(pHead,pos1,val1);
    traverse_list(pHead);
    int pos2;
    printf("删除的位置:");
    scanf("%d",&pos2);

    delete_list(pHead,pos2);
    traverse_list(pHead);

    return 0;
}

//交换算法
void swap(int* a,int * b){
   int tmp  = *a; 
   * a  = *b;
   * b = tmp ;
}

//创建一个链表
PNODE create_list(){
    int len;//length of list
    printf("请输入链表的长度:");
    scanf("%d",&len);
    int value;//结点的值
    //分配一给不存放数据的头结点
    PNODE pHead = (PNODE)malloc(sizeof(NODE));
    if(pHead == NULL){
        printf("分配终止,程序停止!");
        exit(-1);
    }
    PNODE pTail = pHead;
    pTail->pNext = NULL;

    for (int i = 0; i < len; i++){
        printf("请输入第%d个结点的值:",i+1);
        scanf("%d",&value);

        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if(NULL == pNew){
            printf("分配失败,程序终止!");
            exit(-1);
        }
        pNew->data = value;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead;
}

//遍历链表
void traverse_list(PNODE pHead){
    if(is_empty(pHead)){
        printf("链表为空!");
    }else{
        PNODE p = pHead->pNext;
        while(NULL != p){
            printf("%d ", p->data);
            p = p->pNext;
        }
        printf("\n");
    }
}

//判断是否为空
bool is_empty(PNODE pHead){
    if(NULL == pHead->pNext){
        //printf("链表为空!");
        return true;
    }else{
        return false;
    }
}

//求链表长度
int length_list(PNODE pHead){
    PNODE p = pHead->pNext;
    int len = 0;
    while(p != NULL){
        len++;
        p = p->pNext;
    }
    printf("链表长度:%d \n",len);
    return len;

}

//链表排序(快速排序)
/** void sort_list(PNODE pHead,PNODE pEnd){
        if(pHead == NULL || pEnd == NULL){
            return;
        }
        int key = pHead->data;
        PNODE p = pHead;
        PNODE q = pHead->pNext;
        while(q != pEnd){
            if(q->data < key){
                //第一个基值要换成第一个小于key的结点值
                p->data = q->data;
                //基值要往前移一位
                p = p->pNext;
                q->data = p->data;
            }
            q = q->pNext;
        }
        p->data = key;
        sort_list(pHead,p);
        sort_list(p->pNext,pEnd);
        return ;
} */

//冒泡排序
void sort_list(PNODE pHead,int len){
    int i,j,t;
    PNODE p,q;
    for(i = 0,p = pHead->pNext;  i < len-1; i++,p=p->pNext){
        for(j = i+1, q= p->pNext; j< len; j++,q=q->pNext){
            if(p->data > q->data){
                t = p->data;
                p->data = q->data;
                q->data = t;
            }
        }
    }
}

//在pHead所指向链表的第pos个节点的前面插入一个新的结点,该节点的值为val,并且pos的值从1开始
bool insert_list(PNODE pHead, int pos, int val){
    int i = 0;
    PNODE p = pHead;
    //让p指向要插入的结点前面
    while(NULL != p && i < pos-1){
        p = p->pNext;
        ++i;
    }
    //pso的位置只能在链表的范围之内
    if(i > pos-1 || NULL == p){
        return false;
    }
    //检查一切正常开始插入结点
    PNODE pNew = (PNODE)malloc(sizeof(NODE));
    if(NULL == pNew){
        printf("动态内存分配失败!");
        exit(-1);
    }
    pNew->data = val;
    pNew->pNext = p->pNext;
    p->pNext = pNew;

    return true;
}

//在pHead所指向链表的第pos个节点处删除一个结点,该节点的值为val,位置为pos
bool delete_list(PNODE pHead, int pos){
    int i = 0;
    //用来保存删除的值
    int Val;
    PNODE p = pHead;
    //让p指向要删除的结点前面
    while(NULL != p && i < pos-1){
        p = p->pNext;
        ++i;
    }
    //pso的位置只能在链表的范围之内
    if(i > pos-1 || NULL == p || p->pNext == NULL){
        return false;
    }
    PNODE q = p->pNext;
    //存储删除的节点值
    Val = q->data;
    p->pNext = q->pNext;
    free(q);
    return true;
}

标签:int,list,改查,链表,PNODE,pHead,增删,data,pNext
来源: https://blog.csdn.net/qq_43378689/article/details/120920981