编程实现一个单链表实现创建、测长以及打印

    xiaoxiao2021-04-13  50

    #include <iostream> #include <string> #include <conio.h> #include <cstdlib> using namespace std; typedef struct student { int data; student *next; }node; node *creat() { node *head,*p,*s; int x=1,cycle=1; head=(node *)malloc(sizeof(node)); p=head; while(cycle) { cin>>x; if(x!=0) { s=(node *)malloc(sizeof(node)); s->data=x; cout<<x; p->next=s; p=s; } else cycle=0; } head=head->next; p->next=NULL; return (head); } int length(node *head) { int n=0; node *p; p=head; while(p!=NULL) { n++; p=p->next; } return n; } void print(node *head) { node *p; int n; n=length(head); p=head; if(head!=NULL) { while(p!=NULL) { cout<<p->data<<" "; p=p->next; } } } int main() { node * A; A=creat(); length(A); print(A); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-669433.html

    最新回复(0)