Data structure-linked list C language implementation

Source: Internet
Author: User

#include <stdio.h>

#include <stdlib.h>

typedef struct NODE

{

int data;//data field

struct Node * pnext;//pointer field

}node, *pnode;

Pnode create_list (void) {

int len;//The number of valid nodes to hold

int i;//The value of the node used to temporarily store user input

The value of the INT val;//node

Pnode Phead = (pnode) malloc (sizeof (NODE));

if (NULL = = Phead) {

printf ("Allocation failed, program terminated!") ");

Exit (-1);

}

printf ("Please enter the number of linked lists you want to enter");

scanf ("%d", &len);

Pnode ptail = Phead;

Ptail->pnext = Null;//ptail always points to the tail node

for (i=0; i<len; ++i) {

printf ("Please enter the value of%d nodes:", i+1);

scanf ("%d", &val);

Pnode pnew = (pnode) malloc (sizeof (NODE));

if (NULL = = pnew)

{

printf ("Allocation failed, program terminated!") ");

Exit (-1);

}

Pnew->data = val;

Ptail->pnext = pnew;

Pnew->pnext = NULL;

Ptail = pnew;

}

return phead;

}

void Traver_list (Pnode phead) {

Pnode ptmp = phead;//Set a temporary node for traversing

while (1) {

if (Ptmp->pnext = = NULL) {

Return

}

printf ("%d", ptmp->pnext->data);

Ptmp->pnext = ptmp->pnext->pnext;

}

}

int main (int argc, const char * argv[]) {

Pnode Phead = null;//equivalent to struct Node *phead = NULL;

Phead = Create_list ();//create a non-circular single-linked list and pay the first address to Phead

Traver_list (Phead);//Iterate through the list

return 0;

}

Data structure-linked list C language implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.