RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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://sczitong.cn/article/hgodc.html