【C语言】链表的正向输出及链表的逆向输出-创新互联
#include#include#define N 20
typedef struct num
{int num;
struct num *next;
} *Link, Linknode;
Link Create_Link(int n);
Link Reverse_Link(Linknode *head); //链表的逆置输出
void Print_Link(Linknode *head);
void Print_Stack(Linknode *head);
int main(void)
{Link head, reverse;
head = Create_Link(N);
printf("\nBefore: ");
Print_Link(head);
reverse = Reverse_Link(head);
// printf("\nAfter: ");
// Print_Stack(reverse);
return 0;
}
Link Create_Link(int n)
{int i;
Link head;
Linknode *p1, *p2;
head = (Link)malloc(sizeof(Linknode));
head->next = NULL;
p2 = head;
while ( i< n )
{p1 = (Linknode *)malloc(sizeof(Linknode));
p1->num = i;
p1->next = NULL;
p2->next = p1;
p2 = p1;
i++;
}
return head;
}
void Print_Link(Linknode *head)
{Linknode *p;
p = head->next;
while ( p != NULL )
{printf("%d ", p->num);
p = p->next;
}
}
Link Reverse_Link(Linknode *head)
{Link stack, p1;
Linknode *p2;
p1 = head;
stack = (Link)malloc(sizeof(Linknode));
stack->next = NULL;
while ( p1->next != NULL )
{p2 = (Linknode *)malloc(sizeof(Linknode));
p2->num = p1->next->num;
p2->next = stack->next;
stack->next = p2;
p1->next = p1->next->next;
}
printf("\nAfter: ");
while ( stack->next != NULL )
{Linknode *k;
k = stack->next;
printf("%d ", k->num);
stack->next = stack->next->next;
free(k);
}
printf("\n");
}
//void Print_Stack(Linknode *head)
//{// int num;
// while ( head->next != NULL )
// {// Linknode *s;
// s = head->next;
// num = s->num;
// printf("%d ", num);
// head->next = head->next->next;
// }
//}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比铜梁网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式铜梁网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖铜梁地区。费用合理售后完善,十余年实体公司更值得信赖。网页标题:【C语言】链表的正向输出及链表的逆向输出-创新互联
网页地址:http://myzitong.com/article/coiois.html