One-way circular link List C language implementation

Source: Internet
Author: User

As we all know, the one-way linked list finally points to null, that is, the one-way loop linked list is not pointing to null, pointing to the head node, so the following program runs the result is that you will see the link list is a dead loop, because it does not point to null, but also the execution of the cycle. String into a ring type.

#include <stdio.h> #include <stdlib.h>typedef struct Node{char name[20];struct node *link;}     Student;student * creat (int n)/* Create a single-linked list of functions, parameter n is the number of people */{/* *h save a pointer to the table header node, *p points to the previous node of the current node, *s points to the current node */student *p,*h,*s; int I;if ((h= (Student *) malloc (sizeof (student)))/* Allocates space and detects ==null ("Cannot allocate memory space!"); Exit (0);} h->name[0]= ' + '; /* Place the data field of the header node empty */h->link=null; /* */p=h;/*p the Link field of the header node to the header node */for (i=0;i<n;i++) {if (s= (student *) malloc (sizeof (student))) */* Allocate new storage space and detect */ {printf ("Cannot allocate memory space!"); Exit (0);} p->link=s; /* Assign the address of S to the link of the node that P points to, so that the nodes that P and s point to are connected together */printf ("Please enter the name of person%d", i+1);//point to the data scanf in the struct ("%s", s->name); S->link =null;p=s;} If this is a one-way list, this is null, and the ring list needs to point to the pointer that holds the table head node. p->link=h; return (h);} int main (void) {int number; student *head;/*head is a pointer to the address of the single-linked header node */student *p;printf ("Please enter the appropriate people: \ n"); scanf ("%d",& number); head=creat (number);    /* Assign the newly created single-Link header address to the Head*/p=head; while (P->link) {printf ("%s\n", P->name);p =p->link;} return 0;}
Operation Result:


One-way circular link 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.