顺序逆序建链表

顺序建表:

#include <stdio.h>
#include <stdlib.h>
struct node{
    int data;
    struct node *next;
};

int main(){

int n,i;
struct node *p,*head,*tail;
scanf("%d",&n);
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;

for(i=0;i<n;i++){
    p=(struct node*)malloc(sizeof(struct node));
    scanf("%d",&p->data);
    p->next=NULL;
    tail->next=p;
    tail=p;
}
p=head->next;
while(p){
    if(p->next!=NULL){
        printf("%d ",p->data);
    }
    else
        printf("%d\n",p->data);

    p=p->next;
}
return 0;
}

逆序建表:

#include <stdio.h>
#include <stdlib.h>
struct node{
    int data;
    struct node *next;
};

int main(){

int n,i;
struct node *p,*head;
scanf("%d",&n);
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;

for(i=0;i<n;i++){
    p=(struct node*)malloc(sizeof(struct node));
    scanf("%d",&p->data);
    p->next=head->next;
    head->next=p;

}
p=head->next;
while(p){
    if(p->next!=NULL){
        printf("%d ",p->data);
    }
    else
        printf("%d\n",p->data);

    p=p->next;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。 首先,我们需要定义一个双向循环链表的结构体,其中包括一个指向前一个节点的指针prev,一个指向后一个节点的指针next以及一个整数值val表示节点的值。 ```c struct Node { int val; struct Node* prev; struct Node* next; }; ``` 然后,我们可以定义一个函数来创一个新节点: ```c struct Node* createNode(int val) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->val = val; newNode->prev = NULL; newNode->next = NULL; return newNode; } ``` 接下来,我们可以定义一个函数来插入节点到双向循环链表的头部: ```c void insertAtHead(struct Node** head, struct Node** tail, int val) { struct Node* newNode = createNode(val); if (*head == NULL) { *head = newNode; *tail = newNode; (*head)->prev = *tail; (*head)->next = *tail; (*tail)->prev = *head; (*tail)->next = *head; } else { newNode->next = *head; newNode->prev = (*head)->prev; (*head)->prev = newNode; (*tail)->next = newNode; *head = newNode; } } ``` 上述函数的参数包括双向循环链表的头部指针和尾部指针以及要插入的节点的值。如果链表为空,则将新节点作为头部和尾部节点,并将其前驱和后继指针都指向自己。如果链表不为空,则将新节点插入到头部,并更新相关的指针。 接下来,我们可以定义一个函数来插入节点到双向循环链表的尾部: ```c void insertAtTail(struct Node** head, struct Node** tail, int val) { struct Node* newNode = createNode(val); if (*tail == NULL) { *head = newNode; *tail = newNode; (*head)->prev = *tail; (*head)->next = *tail; (*tail)->prev = *head; (*tail)->next = *head; } else { newNode->prev = *tail; newNode->next = (*tail)->next; (*tail)->next = newNode; (*head)->prev = newNode; *tail = newNode; } } ``` 上述函数的参数与插入头部的函数相同,只是将新节点插入到尾部,并更新相关的指针。 最后,我们可以定义一个函数来顺序逆序输出链表: ```c void printList(struct Node* head) { struct Node* curr = head; printf("顺序输出链表:\n"); do { printf("%d ", curr->val); curr = curr->next; } while (curr != head); printf("\n逆序输出链表:\n"); curr = head->prev; do { printf("%d ", curr->val); curr = curr->prev; } while (curr != head->prev); } ``` 上述函数的参数是链表的头部指针,首先按顺序输出链表中的节点值,然后按逆序输出链表中的节点值。 最后,我们可以在主函数中使用以上函数来读入一系列整数,并依次插入到双向循环链表的头部和尾部,然后顺序逆序输出链表: ```c int main() { struct Node* head = NULL; struct Node* tail = NULL; int val; printf("请输入一系列整数(以-1结束):\n"); while (scanf("%d", &val) != EOF && val != -1) { insertAtHead(&head, &tail, val); insertAtTail(&head, &tail, val); } printList(head); return 0; } ``` 上述代码中,我们使用了scanf()函数从标准输入读入一系列整数,并在每次读入后将其插入到链表的头部和尾部。最后,我们调用printList()函数来输出链表中的节点值。 希望能够帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值