#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语言链表的头插,尾插,遍历,以及交换末尾值-创新互联
转载注明:https://www.cdcxhl.com/article12/hgodc.html
成都网站建设公司_创新互联,为您提供移动网站建设、外贸网站建设、网站营销、网站维护、App开发、标签优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联