/** @Author: suifengtec* @Date: 2017-09-02 16:06:33* @Last modified by: suifengtec* @Last Modified time: 2017-09-02 20:47:13**//* character unidirectional list gcc -o a.exe main.c && a */#include <stdio.h > #include <stdlib.h> #include <stdbool.h>// declare a self-contained struct listnode and define it as a type Listnodetypedef struct listnode{char data; struct listnode *nextptr;} ListNode;// define a type for the ListNode pointer listnodeptrtypedef listnode *listnodeptr ;// Boot description void bootstrap (void);// inserted into the linked list, where the// parameters are inserted according to the character's ASCII code, respectively, the pointer of the current node and the value of the element void insert (Listnodeptr *sptr, char value);// whether the linked list is empty bool isempty (ListNodePtr SPTR);// removes a specific element value from the list, returns whether to delete the successful Boolean value// only 1 at a time, even if there are two aa, in the list in value A , Only deletes the first bool delete (LISTNODEPTR&NBSp;*sptr, char value);// Print List void printlist (listnodeptr currentptr); Int main ( void) {// start pointer/current pointer listnodeptr startptr = null;// in the boot instructions, can only take 1,2,3 A value in the other integer will be considered a character unsigned int choice;// character char item;// boot description bootstrap ();// Get user-entered 1 or 2 or 3 or characters, or no spaces without 1 and 2 and 3 strings of printf ("%s", "?"); scanf ("%u", &choice);// not 3 when while (choice!=3) {switch (choice) {// 1 means inserting characters into the list case 1:/* input one character at a time also supports entering a string with no spaces, such as a hellochina! linked list!-->c-->h-->a-->h-->i-->i-->null */ printf ("%s", "Enter a character:"), scanf ("\n%c", &item), insert (&startptr,item);p rintlist (startptr);break;// 2 indicates that a character is removed from the linked list, the linked list is empty, or there are no characters to delete, giving an error. If the character Embox in this string exists Yu Xinzu,// can also delete the input string without spaces of 1 and 2 and 3 at once, Case 2:if (!isempty (startptr)) {printf ("%s", " enter a character to be deleted: "); scanf (" \n%c ", &item); if (dElete (&startptr, item)) {printf ("\ '%c\ ' has be deleted.\n", item);p rintlist ( STARTPTR);} else{printf ("not found \ '%c\ ' \ n", item);}} Else{puts ("The list is empty?");} break;// Reserve, because impossible to case 3:puts here ("invalid option!"); Bootstrap (); break;} /*bootstrap (); */printf ("%s", "?"); scanf ("%u", &choice);} return 0;} Void bootstrap (void) {printf ("enter your choice:\n" "1 = insert an Element to the list\n "" 2 = delete an element from the list\n "3 = game over.\n");} Void insert ( ListNodePtr *sPtr, char value ) {listnodeptr newptr = malloc (sizeof (ListNode)); if ( newPtr != NULL ) {newptr->data = value; newptr->nextptr = null; listnodeptr preptr = null; Listnodeptr currentptr = *sptr;whilE ( currentptr != null && value > currentptr->data) {PREPTR = currentptr;currentptr = currentptr->nextptr;} if (preptr==null) {newptr->nextptr = *sptr;*sptr = newptr;} Else{preptr->nextptr = newptr;newptr->nextptr = currentptr;}} else{printf ("%c not inserted. no memory availabe.\n", value);}} Bool delete (Listnodeptr *sptr, char value) {if (value == (*SPTR)->data) { listnodeptr tmpptr = *sptr;*sptr = (*sptr)->nextptr;free (TMPPTR); return true;} else{listnodeptr preptr = *sptr; listnodeptr currentptr = (*sptr)->nextptr;while (currentptr != null && currentptr->data != value) {preptr = currentptr;currentptr = currentptr- >nextptr;} if (currentptr!=null) {Listnodeptr tmpptr = currenTptr;preptr->nextptr = currentptr->nextptr;free (tmpptr); return true;}} Return false;} Bool isempty (listnodeptr sptr) {return sptr == null?true:false;} Void printlist (listnodeptr currentptr) {if (IsEmpty (currentptr)) {puts ("List is empty?");} Else{puts ("the list is :\n"), while (Currentptr->nextptr!=null) {printf ("%c-->", Currentptr->data); currentptr = currentptr->nextptr;} Puts ("null\n");}} /*eof*/
C language implementation of one-way character Fu Chinqu.
C language character one-way list