Description
Create a single-linked list of length n, then invert its data elements, that is, the 1th element becomes the last element, the 2nd element becomes the 2nd element, ..., and the last element becomes the 1th element. (The data type processed is a character type.) You must use a linked list to complete. )
Input
The first behavior is the list length n; the value of n data elements in the second behavior list.
OutputThe original value after the inverse. Sample input10a B C D E F G H I Sample Output I H G F E D C B A
Analysis: Inverse system, only need to use the head interpolation method to establish a single-linked list;
Code:
#include <stdio.h>
#include <malloc.h>
typedef struct NODE
{
Char str[100];
struct Node*next;
}node;
void Create (node*&l,int N)//head interpolation
{
node*s;
int i;
L= (node*) malloc (sizeof (Node));
l->next=null;
for (i=0;i<n;i++)
{
S= (node*) malloc (sizeof (Node));
scanf ("%s", S->STR);
s->next=l->next;
l->next=s;
}
}
int main ()
{
Node*l;
int total;
scanf ("%d", &total);
Create (l,total);
Node*read;
read=l->next;
while (read)
{
printf (read->next? ") %s ":"%s", read->str);//output format, control last one more space
read=read->next;
}
return 0;
}
Reverse single-linked list (0957) Swust-oj