链表的创建、插入、删除、输出、逆转

链表的创建和输出

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

typedef struct snode{
    int data;
    struct snode *next;
}node;

node *creatlist(int n);
void display(node *head);

int main(){
    int n=0;
    node *head=NULL;
    printf("please enter the number");
    scanf("%d",&n);
    head=creatlist(n);
    display(head);
    return 0;
}

node *creatlist(int n){     //the function to creat the linklist
    int i=0;
    node *head=NULL;
    node *temp=NULL;
    node *p=NULL;
    for(i=0;i<n;i++){       //creat the isolated individual notes
        temp=(node *)malloc(sizeof(struct snode));
        //printf("please input the data");
        scanf("%d",&(temp->data));
        temp->next=NULL;
        if(head==NULL){
            head=temp;
            p=temp;
        }
        else
        {
            p->next=temp;
            p=temp;
        }
    }
    return head;
}

void display(node *head){       //print the linkedlist
    node *p=head;
    while(p!=NULL){
        printf("%d->",p->data);
        p=p->next;
    }
}


链表的插入和逆转

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

typedef struct snode{
    int data;
    struct snode *next;
}node;

node *creatlist(int n);
void display(node *head);
void insertnode(node *head,int data,int pos);
void delete(node *head,int pos);
node *reverse1(node *head);
void printbackward(node *head);

int main(){
    int n=0;
    node *head=NULL;
    printf("please enter the number");
    scanf("%d",&n);
    head=creatlist(n);
    display(head);      //创建完打印
    printf("after inserting:");
    insertnode(head, 8, 3);
    display(head);      //插入完打印
    printf("after delete:");
    delete(head,3);
    display(head);      //删除完打印
    printf("after reversed:");
    reverse1(head);
    display(head);      //反转完打印
    printbackward(head);
    display(head);
    return 0;
}

node *creatlist(int n){     //the function to creat the linklist
    int i=0;
    node *head=NULL;
    node *temp=NULL;
    node *p=NULL;
    for(i=0;i<n;i++){       //creat the isolated individual notes
        temp=(node *)malloc(sizeof(struct snode));
        //printf("please input the data");
        scanf("%d",&(temp->data));
        temp->next=NULL;
        if(head==NULL){
            head=temp;
            p=temp;
        }
        else
        {
            p->next=temp;
            p=temp;
        }
    }
    return head;
}

//print the linkedlist,the function will not return anything,we use the type void.

void display(node *head){
    node *p=head;
    while(p!=NULL){
        printf("%d->",p->data);
        p=p->next;
    }
    printf("\n");
}

//the function will not return anything,we use the type void.

void insertnode(node *head,int data,int pos){       //insert the node
    node *p=head;
    node *temp=(node *)malloc(sizeof(struct snode));
    temp->data=data;        //赋值
    int i=1;
    while(i<pos){
        if(!p->next){   //the possible the linklist is empty
            return;
        }
        p=p->next;
        i++;
    }
    temp->next=p->next;
    p->next=temp;
}

//delete pos位置的值

void delete(node *head,int pos){
    if(pos<=0){
        return;
    }
    node *p=head;
    node *temp=(node *)malloc(sizeof(struct snode));
    int i=0;
    while(i<pos-1){
        p=p->next;
        i++;
        }
    temp=p->next;
    p->next=temp->next;
    free(temp);
}

//reverse the linklist

node *reverse1(node *head){
    if(head==NULL||head->next==NULL){
        return head;
    }else{
        node *p=reverse1(head->next);
        //p->next=head;
        head->next->next=head;
        head->next=NULL;
        return p;
    }
}

node *reverse2(node *head){
    if(head==NULL||head->next==NULL){
        return head;
    }
    node *pre=NULL;
    node *aft=head;
    node *temp=NULL;
    while(aft!=NULL){
        temp=aft->next;
        aft->next=pre;
        pre=aft;
        aft=temp;
    }
    return pre;
}


//print the data backward

void printbackward(node *head){
    if(head==NULL|| head->next==NULL){
        return;
    }
    printbackward(head->next);
    printf("%d->",head->data);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值