数据结构之链表

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

typedef struct ListNode{
    int data;
    struct ListNode *next;
}ListNode;

typedef struct List{
    ListNode head; //变量,不是指针,方便链表节点的插入和删除,虚拟头节点(没有数据域)
    int length; //当前链表的长度信息
}List;
//初始化结点
ListNode *getNewNode(int );
//初始化链表
List *getLinkList();
//删除结点
void clear_node(ListNode *);
//删除链表
void clear(List *);
//链表的插入
int insert (List *,int ,int );
//链表的删除
int erase(List *,int );
//reverse
void reverse(List *l);
//输出
void output(List *);


ListNode *getNewNode(int val){
    ListNode *p=(ListNode *)malloc(sizeof(ListNode));
    p->data=val;
    p->next=NULL;
    return p;
}

List *getLinkList(){
    List *l=(List *)malloc(sizeof(List));
    l->head.next=NULL;
    l->length=0;
    return l;
}

int insert(List *l ,int ind,int val){
    if(l==NULL)return 0;
    if(ind<0||ind>l->length) return 0;
    //p指向虚拟头节点的地址,防止在头结点插入
    ListNode *p=&(l->head),*node=getNewNode(val);
    while(ind--) p=p->next;
    node->next=p->next;
    p->next=node;
    l->length += 1;
    return 1;
}

int erase(List *l,int ind){
    if(l==NULL) return 0;
    if(ind<0 || ind>=l->length) return 0;
    ListNode *p=&(l->head) ,*q;
    while(ind--){
        p=p->next;
    }
    q=p->next;
    p->next=q->next;
    free(q);
    l->length-=1;
    return 1;
}

void reverse(List *l){
    ListNode *p=l->head.next,*q;
    l->head.next=NULL;
    while(p){
        q=p->next;
        p->next=l->head.next;
        l->head.next=p;
        p=q;
    }
    return;
}

void output(List *l){
    if(l==NULL) return;
    printf("List(%d)=[",l->length);
    for(ListNode *p=l->head.next;p;p=p->next){
        printf("%d->",p->data);
    }
    printf("NULL]\n");
    return;
}

void clear_node(ListNode *node){
    if(node==NULL) return;
    free(node);
    return ;
}

void clear(List *l){
    if(l==NULL) return;
    ListNode *p=l->head.next,*q;
    while(p){
        q=p->next;
        clear_node(p);
        p=q;
    }
    free(l);
    return ;
}

int main(){
    srand(time(0));
    #define max_op 20
    List *l=getLinkList();
    for(int i=0;i<max_op;i++){
        int val=rand()%100;
        //模拟所有输出
        int ind=rand()%(l->length+3)-1;
        int op=rand()%5;//0,1,2,插入,3删除,4反转
        switch(op){
            case 0:
            case 1:
            case 2:{
                printf("insert %d at %d to List = %d\n",val,ind,insert(l,ind, val));
                break;
            }
            case 3:
                printf("erase iterm at %d from List = %d\n",ind ,erase(l,ind));
                break;
            case 4:
                printf("reverse the List !\n");
                reverse(l);
                break;
        }
        output(l);
        printf("\n");
    }
    #undef max_op 
    clear(l);
    return 0;
}

erase iterm at 1 from List = 0
List(0)=[NULL]

insert 9 at 1 to List = 0
List(0)=[NULL]

insert 32 at 0 to List = 1
List(1)=[32->NULL]

insert 62 at 1 to List = 1
List(2)=[32->62->NULL]

insert 26 at -1 to List = 0
List(2)=[32->62->NULL]

insert 76 at 2 to List = 1
List(3)=[32->62->76->NULL]

insert 33 at 4 to List = 0
List(3)=[32->62->76->NULL]

insert 29 at -1 to List = 0
List(3)=[32->62->76->NULL]

insert 75 at 2 to List = 1
List(4)=[32->62->75->76->NULL]

insert 88 at -1 to List = 0
List(4)=[32->62->75->76->NULL]

reverse the List !
List(4)=[76->75->62->32->NULL]

insert 72 at 0 to List = 1
List(5)=[72->76->75->62->32->NULL]

insert 46 at 3 to List = 1
List(6)=[72->76->75->46->62->32->NULL]

insert 36 at 7 to List = 0
List(6)=[72->76->75->46->62->32->NULL]

erase iterm at 7 from List = 0
List(6)=[72->76->75->46->62->32->NULL]

insert 68 at 3 to List = 1
List(7)=[72->76->75->68->46->62->32->NULL]

erase iterm at 3 from List = 1
List(6)=[72->76->75->46->62->32->NULL]

insert 43 at 4 to List = 1
List(7)=[72->76->75->46->43->62->32->NULL]

erase iterm at 0 from List = 1
List(6)=[76->75->46->43->62->32->NULL]

insert 70 at 3 to List = 1
List(7)=[76->75->46->70->43->62->32->NULL]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值