编程建立一个带有头节点的单链表,链表中的数据从键盘输入,当输入数据
1个回答
展开全部
下面是你要的链表:
#include <stdlib.h>
#include <stdio.h>
struct Node {
int value;
Node * next;
};
Node * buildList(int arr[], int n)
{
if(n == 0) return NULL;
Node * head = new Node();
head->value = arr[0];
head->next = NULL;
Node *p = head, *q;
for(int i=1; i<n; i++) {
q = new Node();
q->value = arr[i];
q->next = NULL;
p->next = q;
p = p->next;
}
return head;
}
void printList(Node *head)
{
Node *p = head;
while(p!=NULL) {
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}
int main()
{
int n , a[100];
printf("输入节点的个数:");
scanf("%d", &n);
printf("输入节点的值:");
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
Node * list = buildList(a, n);
printList(list);
//float x;
//scanf("%f", &x);
}
#include <stdlib.h>
#include <stdio.h>
struct Node {
int value;
Node * next;
};
Node * buildList(int arr[], int n)
{
if(n == 0) return NULL;
Node * head = new Node();
head->value = arr[0];
head->next = NULL;
Node *p = head, *q;
for(int i=1; i<n; i++) {
q = new Node();
q->value = arr[i];
q->next = NULL;
p->next = q;
p = p->next;
}
return head;
}
void printList(Node *head)
{
Node *p = head;
while(p!=NULL) {
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}
int main()
{
int n , a[100];
printf("输入节点的个数:");
scanf("%d", &n);
printf("输入节点的值:");
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
Node * list = buildList(a, n);
printList(list);
//float x;
//scanf("%f", &x);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询