读书人

帮忙补充上这道代码!关于linklist的

发布时间: 2012-10-11 10:16:10 作者: rapoo

帮忙补充下这道代码!关于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 listBoolean insert( char *new_string );// empty the list so that we clear all memory and can start a fresh listvoid clearList();// tells us whether or not the given string is in the listBoolean search( char *target );// starts a list traversal by getting the data at topchar * firstItem();// gets the data at the current traversal node and increments the traversalchar * nextItem();//-------------------------------------------------// FUNCTIONSBoolean 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 listvoid 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 outputvoid 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;} 


[解决办法]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//-------------------------------------------------
// CONSTANTS and TYPES
//-------------------------------------------------

typedef enum BOOL { false, true } Boolean;

typedef struct NODE node;






//-------------------------------------------------
// VARIABLES
//-------------------------------------------------
struct NODE {
NODE* F;
NODE* B;
char* str;
};

//-------------------------------------------------
// 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)
{
NODE* new_node,temp;
new_node->str = new_string;
temp = node->F;
new_node->B = node;
new_node->F = temp;
node->F = new_node;
temp->B =new_node;
return true;
}
void clearList()
{
node->F = node;
node->B = node;
}

Boolean search(char *target)
{
NODE* temp;
char* str1;
for (str1 = firstItem(); str1 != '\0'; ){
if (str1 == target)
return true;
else
str1 = nextItem();
}
return false;

}

读书人网 >C语言

热点推荐