链表的输入输出及逆转
2017-02-20 18:35
267 查看
对于链表,输入输出以及逆转是比较常见的,输入输出时可以根据自己的需要调整,我这写的c程序是输入一个链表的长度,然后输入链表各节点数据,最后输出顺序的数据以及逆序的数据。给了三个函数,分别是输入输出以及逆转的程序。
先给出一个输入输出函数:
#include<stdio.h>
#include<stdlib.h>
typedef struct node * List;
struct node
{
int data;
List next;
};
List read()
{
int len,num;
List first,second,third;
scanf("%d",&len);
scanf("%d",&num);
first=(List)malloc(sizeof(struct node));
first->data=num;
first->next=NULL;
second=first;
len--;
while(len--)
{
scanf("%d",&num);
third=(List)malloc(sizeof(struct node));
third->data=num;
third->next=NULL;
second->next=third;
second=third;
}
return first;
}
int main()
{
List L1;
L1=read();
while(L1->next!=NULL)
{
printf("%d ",L1->data);
L1=L1->next;
}
printf("%d\n",L1->data);
return 0;
}
运行如图。
单链表逆转也和上面类似,多了一个逆转过程:
#include <stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
struct Node {
ElementType Data;
PtrToNode Next;
};
int main()
{
List Read();
void Print( List L );
List Reverse( List L ); //函数声明
List L1, L2;
L1 = Read();
Print(L1);
L2 = Reverse(L1); //如果将这一行和上面一行交换位置,就会不一样,输出L1的时候就会只有一个数。
Print(L2);
return 0;
}
List Read()
{
int len;
int num;
List list;
List last;
List node;
scanf( "%d",&len );
if( len == 0 )
return NULL;
scanf( "%d",&num );
list = ( PtrToNode )malloc( sizeof( struct Node ) );
list->Data = num;
list->Next = NULL;
last = list;
len--;
while( len-- ){
scanf( "%d",&num );
node= ( List )malloc(sizeof(struct Node));
node->Data = num;
node->Next = NULL;
last->Next = node;
last = node;
}
return list;
}
vo
4000
id Print( List L )
{
if(L==NULL)
return ;
else
while(L!=NULL){
printf("%d ",L->Data);
L=L->Next;
}
putchar('\n');
}
List Reverse( List L ){
PtrToNode t=NULL;
PtrToNode newlist=NULL;
while(L!=NULL){
t=L->Next; //用t保存L的下一个节点,否则L->next就丢失了,方便L的移动
L->Next=newlist;//L指向它的前一个节点
newlist=L; //newlist指向已经逆转的最后一个节点
L=t; //将L移动到下一个元素
}
return newlist;
}
先给出一个输入输出函数:
#include<stdio.h>
#include<stdlib.h>
typedef struct node * List;
struct node
{
int data;
List next;
};
List read()
{
int len,num;
List first,second,third;
scanf("%d",&len);
scanf("%d",&num);
first=(List)malloc(sizeof(struct node));
first->data=num;
first->next=NULL;
second=first;
len--;
while(len--)
{
scanf("%d",&num);
third=(List)malloc(sizeof(struct node));
third->data=num;
third->next=NULL;
second->next=third;
second=third;
}
return first;
}
int main()
{
List L1;
L1=read();
while(L1->next!=NULL)
{
printf("%d ",L1->data);
L1=L1->next;
}
printf("%d\n",L1->data);
return 0;
}
运行如图。
单链表逆转也和上面类似,多了一个逆转过程:
#include <stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
struct Node {
ElementType Data;
PtrToNode Next;
};
int main()
{
List Read();
void Print( List L );
List Reverse( List L ); //函数声明
List L1, L2;
L1 = Read();
Print(L1);
L2 = Reverse(L1); //如果将这一行和上面一行交换位置,就会不一样,输出L1的时候就会只有一个数。
Print(L2);
return 0;
}
List Read()
{
int len;
int num;
List list;
List last;
List node;
scanf( "%d",&len );
if( len == 0 )
return NULL;
scanf( "%d",&num );
list = ( PtrToNode )malloc( sizeof( struct Node ) );
list->Data = num;
list->Next = NULL;
last = list;
len--;
while( len-- ){
scanf( "%d",&num );
node= ( List )malloc(sizeof(struct Node));
node->Data = num;
node->Next = NULL;
last->Next = node;
last = node;
}
return list;
}
vo
4000
id Print( List L )
{
if(L==NULL)
return ;
else
while(L!=NULL){
printf("%d ",L->Data);
L=L->Next;
}
putchar('\n');
}
List Reverse( List L ){
PtrToNode t=NULL;
PtrToNode newlist=NULL;
while(L!=NULL){
t=L->Next; //用t保存L的下一个节点,否则L->next就丢失了,方便L的移动
L->Next=newlist;//L指向它的前一个节点
newlist=L; //newlist指向已经逆转的最后一个节点
L=t; //将L移动到下一个元素
}
return newlist;
}
相关文章推荐
- 输入一个链表,反转链表后,输出链表的所有元素。就地逆转,头插法
- 链表的输入输出以及就地逆转
- 11. 微软面试题:输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针
- 面试9之输入一个链表,输出该链表中倒数第k个结点。
- java 输入一个链表,反转链表后,输出链表的所有元素。
- 输入一个链表的头结点,从尾到头反过来输出每个结点的值
- 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。C++
- 输入一个链表,输出该链表中倒数第k个结点
- C语言链表的输入输出
- 题目:输入一个链表的头结点,从尾到头反过来输出每个结点的值
- 输入一个链表,反转链表后,输出链表的所有元素。
- 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则
- 总结体验1:就比如上面自己写的单链表建立这样的程序,其实明明知道输出单链表不能从头结点开始输入,而是要在第一个节点开始
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- 面试题:输入一个链表,输出该链表中倒数第k个结点。
- 输入一个链表,输出该链表中倒数第k个结点。
- 输入一个链表的头结点,从尾到头反到来输出每个结点的值。
- c++链表 建立 输入 输出 删除结点 插入结点 释放链表 的函数
- 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
- 剑指offer--输入一个链表,输出该链表中倒数第k个结点。