单链表根据尾插法建立

include<stdio.h>

include<stdlib.h>

typedef struct slik{
int data;
struct slik* next;
}sli;

void createsli(sli** head, int a[],int size){

for(int i=0;i<size;i++){
    sli* s=(sli*)malloc(sizeof(sli));
    s->data=a[i];
    s->next=NULL;
     if (*head == NULL) {
    *head = s;
} else {
    // Find the last node
    sli* last = *head;
    while (last->next != NULL) {
        last = last->next;
    }
    // Append the new node at the end
    last->next = s;



}
}

}
void ppeet(sli **head){
while((head)!=NULL){
printf("%d ",(
head)->data);
head=(head)->next;
}

}
int main(){
sli* head= NULL;
int a[]={4,5,6,4,6,4,6,3,6,3};
createsli(&head,a,10);
ppeet(&head);

}

posted on 2024-04-26 20:46  jjjkkklll  阅读(5)  评论(0编辑  收藏  举报

导航