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

include<stdio.h>

include<stdlib.h>

typedef struct slik{
int data;
struct slik* next;
struct slik* prev;
}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;
    s->prev=NULL;
     if (*head == NULL) {
    *head = s;
} else {
    // Find the last node
   
    // Append the new node at the end
   s->next = *head;
        (*head)->prev = s;
        *head = s;
}
  

}

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

}
// 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 09:04  jjjkkklll  阅读(2)  评论(0编辑  收藏  举报

导航