单向循环链表的头插法实现

``#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;
   

}


}

sli* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = *head;

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

// }
void ppeet(sli *head) {
sli current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
}
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-27 08:22  jjjkkklll  阅读(2)  评论(0编辑  收藏  举报

导航