Home > Back-end >  Small white questions about singly linked lists to create and tail of interpolation
Small white questions about singly linked lists to create and tail of interpolation

Time:10-04

#include
#include
#include

Typedef char DataType.

Typedef struct LinkList
{
DataType data;
Struct LinkList * next;
} LinkList;//define node

Void InsertDataByTail (LinkList * head)//stern interpolation
{
Char x;
LinkList * node;
LinkList * remove;

While (NULL!=the head - & gt; Next)
{
The head=head - & gt; next;
}

Remove=head;

Printf (" input the data (0 stop) : \ n ");
X=getchar ();
While (x!='0')
{
The node=(LinkList *) malloc (sizeof (LinkList));
Node - & gt; Data=https://bbs.csdn.net/topics/x;
Node - & gt; Next=remove - & gt; next;
Remove - & gt; Next=node;
Remove=node;
X=getchar ();
}
}

Void ShowListInfor (LinkList * head)
{
The head=head - & gt; next;

Printf (" output list \ n ");
While (NULL!=head)
{
Putchar (head - & gt; The data);
The head=head - & gt; next;
}
}

Void GetListLength (LinkList * head)
{
int count=0;

While (the head - & gt; Next) {
The head=head - & gt; next;
count++;
}

Printf (" the length is % d \ n ", the count/2);
}

Void main ()
{
LinkList * head;
Head=(LinkList *) malloc (sizeof (LinkList));
The head - & gt; Next=NULL;
InsertDataByTail (head);
ShowListInfor (head);
GetListLength (head);
}

Please input: a b c d e

The chain length of the table is twice the length of the right, let me have to count in GetListLength function/2,

Overall is want to create a head node, tail plug was used to establish a linked list, insert the data and create a linked list,

Why is two times, really don't understand why the count

CodePudding user response:

Because of you this kind of writing is double
A is a node, then press enter is a node, b is a point, enter another node
X=getchar () is first to get to the character, acquisition of a second return ah, so on

CodePudding user response:

Fun
reference 1 floor response:
for you this kind of writing is a double
A is a node, then press enter is a node, b is a point, enter another node
X=getchar () is first to get to the character, the acquisition of a second return ah, and so forth

God wants us to meet a few wrong people wake up overnight, thank thank????????????

CodePudding user response:

'\ n' is also a node, so the length is doubled

CodePudding user response:

Use % d input Numbers should not appear this kind of circumstance, getchar () more specific
  • Related