Data link creation and data link output

Source: Internet
Author: User
Tags array length

/*

This program mainly realizes the storage and output of the data chain, and writes the output value in a file.

"The main code of this program is in the definition:

Lnode *p=l->next;//defines a node pointer p points to the next node of the head node

And after the data chain is established, the pointers on the data chain cannot be returned, only the method of modifying the data chain (the method of the methods in the way) is invoked using the method of the creation.


*/



#include <stdio.h>

#include <stdlib.h>


11th Data Link

typedef struct lnode{

int date;

struct Lnode *next;

}lnode;


Outputs a value in a data chain and saves the output values to a file


void Displist (Lnode *l)

{

FILE *f;

f = fopen ("/users/tarena0/desktop/lnode.txt", "w");

Lnode *p=l->next;//defines a node pointer p points to the next node of the head node

while (p) {//if p is not empty then loop

printf ("%d\n", p->date);

fprintf (F, "%d", p->date);

p=p->next;//moving pointer p traversing linked list

}

Fclose (f);

}


Set up a data chain to store the array in the data chain


void Create (Lnode *l, int m[], int n) {

Define two pointers, one to hold the head pointer and the other to create a data link

Lnode *s, *r;

Request a node for a data link

L = (lnode*) malloc (sizeof (Lnode));

R = l;//used to hold the head pointer

L->next = null;//head node empty

for (int i= 1; i<= N; i++) {

s = (Lnode *) malloc (sizeof (Lnode));

Define a Dynamic Data link node that is used to request space and establish a data chain

printf ("Please enter%d numbers:", i);

scanf ("%d", &m[i]);

Data link establishment of tail interpolation method

S->date = M[i];

R->next = s;

r = r->next;

}

R->next = NULL;

Displist (l);

}

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

{

int n;

int m[n];

Lnode *l;

printf ("Please enter array length n:");

scanf ("%d", &n);

Create (L, m, n);//Method call

return 0;

}


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.