文章

28

粉丝

230

获赞

23

访问

230.3k

头像
创建以C为表头结点,再以尾插法建表
P1015 贵州大学机试题
发布于2021年2月19日 15:09
阅读数 8.3k

#include <iostream>
#include <algorithm>

using namespace std;

typedef struct Node {
    int Element; // 节点中的元素为整数类型
    struct Node * Next; // 指向下一个节点
} Node;

int main(int argc, char const *argv[])
{
	int n,i,num;
	int A[5] = {0};
	n=5;
	i=0;
	while(n!=0){
		scanf("%d",&A[i]);
		n--;
		i++;
	}
	sort(A,A+5);
	Node *C=(Node *)malloc(sizeof(Node));
	C->Next = NULL;
	Node *s,*q;
	s=C;
	for (int i = 0; i < 5; ++i){
		q=(Node *)malloc(sizeof(Node));
		q->Element = A[i];
		s->Next=q;
		s=q;//尾插法建表
	}
	s->Next = NULL ;
	Node *p = C->Next ;
	while (p!= NULL){
		printf("%d ",p->Element);
		p=p->Next ;
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发