C语言数据结构-线性表-单链表

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


typedef struct LNode{
    int num;
    struct LNode *next;


}LNode, *LNodeList;


LNodeList init(){
  LNodeList lList=(LNodeList)malloc(sizeof(struct LNode));
  lList->num=0;
  lList->next=NULL;
  return lList;
}


void add(int num,LNodeList lList){
    LNodeList nList=(LNodeList)malloc(sizeof(struct LNode));
    nList->next=NULL;
    nList->num=num;
    LNodeList current=lList;
    while(current->next!=NULL){
        current=current->next;
    }
    current->next=nList;
}


void insert(int num,int index,LNodeList lList){
    int i=0;
    LNodeList newNode=(LNodeList)malloc(sizeof(struct LNode));
    newNode->num=num;
    LNodeList current=lList;
    while(current->next!=NULL){
        if(i==index){
            newNode->next=current->next;
            current->next=newNode;
            break;
        }
        current=current->next;
        i++;
    }
}


void delete(int index,LNodeList lList){
    int i=0;
    LNodeList current=lList->next;
    LNodeList pre=lList;
    while(current!=NULL){
        if(i==index){
            pre->next=current->next;
            free(current);
            break;
        }
        pre=current;
        current=current->next;
        i++;
    }
}


int query(int num,LNodeList lList){
    LNodeList current=lList->next;
    int i=0;
    while(current!=NULL){
        if(current->num==num){
            return i;
        }
        current=current->next;
        i++;
    }
    return -1;
}


void printAll(LNodeList lList){
    LNodeList current=lList->next;
    while(current!=NULL){
        printf("node=%d->",current->num);
        current=current->next;
    }
}
/**
* 带有头结点的单链表实现线性表操作
*/
int main(){
    LNodeList list=init();
    add(5,list);
    add(6,list);
    add(8,list);
    insert(4,0,list);
    insert(7,3,list);
    delete(0,list);
    delete(2,list);
    int index=query(8,list);
    printf("index=%d",index);
    printAll(list);
    return 1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值