#include<iostream>
#include<stdio.h>
using namespace std;
typedef struct listnode{
int data;
listnode * next;
};
listnode * creat(int cycle){
listnode *head, *p, *s;
int x;
head = (listnode *)malloc(sizeof(listnode));
p = head;
while (cycle)
{
cout << "请输入一个值" << endl;
cin >> x;
s = (listnode *)malloc(sizeof(listnode));
s->data= x;
p->next = s;
p = s;
--cycle;
}
head = head->next;
p->next = nullptr;
return head;
}
void printlistnodelen(listnode * head)
{
if (head==nullptr)
{
cout << "listnode is null! " << endl;
}
int count = 0;
while (head)
{
count++;
head = head->next;
}
cout << "length: " << count << endl;
}
void printlistnode(listnode * head)
{
while (head)
{
cout << head->data<< " ";
head = head->next;
}
cout << endl;
}
int main(){
listnode * lnvar = creat(5);
printlistnode(lnvar);
printlistnodelen(lnvar);
system("pause");
return 0;
}
创建单链表及测长、打印
最新推荐文章于 2023-09-24 11:51:51 发布