#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct 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;
}