您的位置:首页 > 编程语言 > C语言/C++

c语言实现正序链表的创建(不使用头结点)

2011-01-25 11:11 681 查看
/**
* c语言实现正序链表的创建(不使用头结点);
*/

#include <stdio.h>
#include <malloc.h>

typedef struct node {
int data;
struct node *next;
}ArrayNode, *ArrayPoint;

void swop(int a[], int aLength) {

ArrayPoint head, p, q;

for(int i=0; i<aLength; i++) {
if(i==0) {
p = (ArrayPoint)malloc(sizeof(ArrayNode));
head = p;
p->data = a[i];
} else {
p->next = (ArrayPoint)malloc(sizeof(ArrayNode));
p = p->next;
p->data = a[i];
}

}
/*链表的打印*/
printf("链表数组为: /n");
for(int i=0; i<aLength; i++) {
printf("%d/n",head->data);
head = head->next;
}
}

main() {
void swop(int a[], int aLength);
int a[] = {2,3,5,6,7,8,11,13,15,19};
int b[] = {1,2,4,5,7,9,10,12,14,20};

swop(a, 10);
swop(b, 10);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  语言 c struct
相关文章推荐