帮忙补充上这道代码!关于linklist的,多谢
帮忙补充下这道代码!关于linklist的,谢谢
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <string.h> #include <stdlib.h> //------------------------------------------------- // CONSTANTS and TYPES //------------------------------------------------- typedef enum BOOL { false, true } Boolean; typedef struct NODE node; //------------------------------------------------- // VARIABLES //------------------------------------------------- //------------------------------------------------- // PROTOTYPES //------------------------------------------------- // add an element to the beginning of the linked list Boolean insert( char *new_string ); // empty the list so that we clear all memory and can start a fresh list void clearList(); // tells us whether or not the given string is in the list Boolean search( char *target ); // starts a list traversal by getting the data at top char * firstItem(); // gets the data at the current traversal node and increments the traversal char * nextItem(); //------------------------------------------------- // FUNCTIONS Boolean insert(char *new_string) { } void clearList() { } Boolean search(char *target) { } //------------------------------------------------- // read from standard input, tokenize and insert words into a linked list // note that we must ensure that there are no duplicates in our list void loadFile() { #define LINE_SIZE 256 char input[LINE_SIZE]; char *token = NULL; FILE *stdin; stdin=fopen("in.txt","r"); while ( fgets( input, LINE_SIZE, stdin ) ) { // parse the data into separate elements token = strtok( input, " \t\n" ); while ( token ) { if ( !search( token ) ) insert( token ); token = strtok( NULL, " \t\n" ); } } } // print the contents of the linked list to standard output void printConcordance() { char *theWord = firstItem(); while ( theWord ) { printf( "%s\n", theWord ); theWord = nextItem(); } } int main( int argc, char *argv[] ) { loadFile(); printConcordance(); clearList(); return EXIT_SUCCESS; }
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <malloc.h> typedef struct node { int data; struct node *next; }linkNode, *linklist; /* *功能:初始化链表 *返回值:链表首地址 */ linklist initList() { linklist head; head = (linklist)malloc(sizeof(linkNode)); if(head == NULL) return NULL; head->next = NULL; return head; } /* *功能:申请空间 *参数:结点(数据) *返回值:指向结点的指针 */ linklist makeNode(linkNode nodeData) { linklist newNode; newNode = (linklist)malloc(sizeof(linkNode)); if(newNode == NULL) return NULL; newNode->data = nodeData.data; return newNode; } /* *功能:输出链表数据 *参数:链表首地址 */ void printList(linklist head) { if(head == NULL || head->next == NULL) return; head = head->next; printf("\nlinklist:\n"); while(head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } /* *功能:在链表尾部插入结点 *参数:链表首地址,待插入结点地址 */ void pushBack(linklist head, linklist insertNode) { if(head == NULL) return; while(head->next != NULL) { head = head->next; } head->next = insertNode; insertNode->next = NULL; } int main() { linklist list, insertNode; linkNode newNode; int i; list = initList(); for(i = 0; i < 10; ++i) { newNode.data = i; insertNode = makeNode(newNode); pushBack(list, insertNode); } printList(list); getchar(); return 0; }