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