快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

c语言链表的头插,尾插,遍历,以及交换末尾值-创新互联

#include#include#includestruct S{
	int data;
	struct S* next;
};
struct S *CreateListL(void){//head_inserting linked list
	struct S *head,*p;
	int data;
	head = NULL;
	scanf("%d",&data);
	while(data != 0){
		p = (struct S *)malloc(sizeof(struct S));
		p->data = data;
		p->next = head;
		head = p;
		scanf("%d",&data);
	}
	return head;
}

struct S*CreateListR(void){//rear_inserting linked list
	struct S *head,*rear,*p;
	int data;
	head = NULL;
	scanf("%d",&data);
	while(data != 0){
		p = (struct S *)malloc(sizeof(struct S));
		p->next = NULL;
		p->data = data;
		if(!head)
			head = p;
		else 
		    rear->next=p;
		rear = p;
		scanf("%d",&data);	
		
	}
	return head;
}
void PrintList(struct S *head){//print the linked list
	struct S *p=head;
	while(p&&p->next!=NULL){
		printf("%d ",p->data);
		p=p->next;
	}	
	printf("%d\n",p->data);// modify it to meet the standard
}

struct S *Exchange(struct S*head){//exchange the last two elements in the linked list
	struct S *p=head;
	while(p->next->next!=NULL){
		printf("%d ",p->data);
		p=p->next;
	}
	struct S *q=p;
	p=p->next;
	printf("%d ",p->data);
	printf("%d ",q->data);
	free(q);
}

int main(){
	int i;
	struct S* p;
	printf("Head Insertion, Data = ");	
	p = CreateListL();
	PrintList(p);
	//尾插法建立链表
	printf("Tail Insertion, Data = ");
	p = CreateListR();
	PrintList(p);// change the last two elements in the linked list
	p=Exchange(p);
	PrintList(p);
	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

创新互联建站成立于2013年,先为宜都等服务建站,宜都等地企业,进行企业商务咨询服务。为宜都企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
当前题目:c语言链表的头插,尾插,遍历,以及交换末尾值-创新互联
分享路径:http://6mz.cn/article/hgodc.html

其他资讯