本文主要是介绍无头结点的单链表的创建和逆置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct node{int data;struct node* next;
}Node;Node* CreateList(void)
{int val,i,n;Node *head,*p,*q;head = NULL;printf("请输入你要建立的链表的长度:\n");scanf("%d",&n);printf("请输入你要输入的数据:\n");for(i=0; i<n; i++){scanf("%d",&val);p = (Node*)malloc(sizeof(Node));p->data = val;if(head == NULL){head=q=p;}elseq->next = p;q=p;}q->next = NULL;return head;
}void showlist(Node* head)
{Node* p;p=head;while(p){printf("%d ",p->data);p = p->next;}printf("\n");
}//链表的逆置
Node* ReverseList(Node* head)
{Node* p,*q,*r;p=head;q=r=NULL;while(p){q = p->next; //q保存的是p下一个结点的指针p->next = r; //p指向rr = p;p = q;}return r;
}int main()
{Node *head;head = CreateList();showlist(head);head = ReverseList(head);showlist(head);//printf("HelloWorld!\n");return 0;
}
运行效果:
这篇关于无头结点的单链表的创建和逆置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!