数据结构基础—c语言简单方法实现链表增删改查

//第一次拿c开始写数据结构,因为自己写的,追求代码量少,和学院ppt不太一样。有错请指出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node//定义节点
{
    int data;
    struct node * next;
}node;
//函数介绍
void printlist(node * head)//打印链表
int lenlist(node * head)//返回链表长度
void insertlist(node ** list,int data,int index)//插入元素
void pushback(node ** head,int data)//尾部插入
void freelist(node ** head)//清空链表
void deletelist(node ** list,int data)//删除元素
node * findnode(node ** list,int data)//查找
void change(node ** list,int data,int temp)//改变值

打印

void printlist(node * head)//打印链表
{
    for(;head!=NULL;head=head->next)printf("%d ",head->data);
    printf("\n");//为了其他函数打印,最后换行
}

链表长度

int lenlist(node * head)//返回链表长度
{
    int len;
    node * temp = head;
    for(len=0; temp!=NULL; len++) temp=temp->next;
    return len;
}

插入元素

void insertlist(node ** list,int data,int index)//插入元素,用*list将head指针和next统一表示
{
    if(index<0 || index>lenlist(*list))return;//判断非法输入
    node * new=(node *)malloc(sizeof(node));//创建
    new->data=data;
    new->next=NULL;
    while(index--)list=&((*list)->next);//插入
    new->next=*list;
    *list=new;
}

尾部增加元素

void pushback(node ** head,int data)//尾插,同上
{
    node * new=(node *)malloc(sizeof(node));//创建
    new->data=data;
    new->next=NULL;
    while(*head!=NULL)head=&((*head)->next);//插入
    *head=new;
}

清空链表

void freelist(node ** head)//清空链表
{
    node * temp=*head;
    node * ttemp;
    *head=NULL;//指针设为空
    while(temp!=NULL)//释放
    {
        ttemp=temp;
        temp=temp->next;
        free(ttemp);
    }
    free(temp);
}

删除

void deletelist(node ** list,int data)//删除链表节点
{
    node * temp;//作用只是方便free
    while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
    if((*list)->data==data){
        temp=*list;
        *list=(*list)->next;
        free(temp);
    }
}

查找

node * findnode(node ** list,int data)//查找,返回指向节点的指针,若无返回空
{
    while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
    if((*list)->data==data)return *list;return NULL;
}

改值

void change(node ** list,int data,int temp)//改变
{
    while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
    if((*list)->data==data)(*list)->data=temp;
}

最后测试

int main(void)//测试
{
    node * head=NULL;
    node ** gg=&head;
    int i;
    for(i=0;i<10;i++)pushback(gg,i);
    printf("链表元素依次为: ");
    printlist(head);
    printf("长度为%d\n",lenlist(head));
    freelist(gg);
    printf("释放后长度为%d\n",lenlist(head));
    for(i=0;i<10;i++)pushback(gg,i);
    deletelist(gg,0);//头
    deletelist(gg,9);//尾
    deletelist(gg,5);
    deletelist(gg,100);//不存在
    printf("再次创建链表,删除节点后\n");
    printlist(head);
    freelist(gg);
    for(i=0;i<5;i++)pushback(gg,i);
    insertlist(gg,5,0);//头
    insertlist(gg,5,5);
    insertlist(gg,5,7);//尾
    insertlist(gg,5,10);//不存在
    printlist(head);
    printf("找到%d\n把3变为100",*findnode(gg,5));
    change(gg,3,100);
    change(gg,11111,1);//不存在
    printlist(head);
}

猜你喜欢

转载自blog.csdn.net/hebtu666/article/details/81261043