从文件输入创建链接列表

时间:2015-02-13 01:31:33

标签: c linked-list

基本上我要做的就是接受一个txt文件并创建一个链接列表。 txt文件包含“i”或“d”,后跟空格,然后是数字。

txt文件示例:

i 10
i 12
d 10
i 5

我正在尝试接收输入文件并根据输入创建链接列表。如果该字母是“i”,我试图将其添加到链接列表中。如果它是“d”,我试图从列表中删除它。

这是我到目前为止所拥有的。我已经接收了文件并逐行读取,将数字与数字分开并将它们存储在char指针中。我之前尝试过创建一个链表,但它没有正常工作。有人可以指出我在哪里以及如何实施链表的正确方向。

#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
#include <string.h>

struct node { // creation of node structure 
        int data; // data inside node
        struct node *next; // address to the next node int the list
        };

void Insert (struct node *root, int x){ // this method will insert a node into the linked list
        struct node *newnode;
        newnode  = (struct node*)malloc(sizeof(struct node));            
        newnode->data=x;
        newnode->next = NULL; // this creates the node and stores the input into the data variable

        if (root == NULL){ // if the root node is empty, point the root node to this pointer.
            root = newnode;
        }
        else {
        newnode->next=root; // if the link list is not empty, set whatever head point was pointing to previously equal to newnode-> next
        root=newnode; // now make the head node point to the newnode
        }
}        

void printlist(struct node *root)
{
    struct node *temp;
    temp=root;
    while(temp!=NULL)
    {
    printf("%d",temp->data);
    temp=temp->next;
    }
    printf("\n");
}           

int main (int argc, char *argv[]){

    // Need to create a Root Node 
    struct node *root;      

    if (argc != 2) { // if the input is more than one, error
        return EXIT_FAILURE;
    }

    // argv[1] will contain the file name input. 

    FILE *file = fopen(argv[1], "r");

    // we need to make sure the file is not empty, error case. 

    if (file == NULL){ 
        printf("error\n");
        exit(0);
    }
    // if the file is empty, print an empty line. ******** MAKE SURE TO COME BACK TO THIS ************


    char linestring[BUFSIZ]; // this will hold our file input after we read/scan it.

    // now we need to read the file

    while (fgets(linestring, sizeof(linestring), file)) // reads the entire file until it hits Null
    { 
        // need to split up the lines so that we separate letters from integars. 
        // look for \t or " " and use strtok. 

        char* letter = strtok(linestring, " \t\n"); // stores the letter from the line
        char* number = strtok(NULL, " \t\n"); // stores the numbers from the line


        // now we need to now check the letter and see whether it is an "i" or a "d"
        // use sting compare (it returns a 0 if the letter is equal to what we are checking)

        if (strcmp(letter, "i") == 0){ // This checks the letter and if it is "i", we are going to be inserting a node

            // need to create the new node to be inserted and assign the number value to it.
            int x;
            x = atoi(number);   // converts the string input into a integar 
            Insert(root, x); // method which inserts the input onto the linked list. 
        }


        if (strcmp(letter, "d") == 0){ // This checks the letter and if it is "d", we are going to be deleting a node
        }
    }

printlist(root);
fclose(file);
}

2 个答案:

答案 0 :(得分:0)

我认为价值不会重复。

struct node 
{ // creation of node structure 
        int data; // data inside node
        struct node *next; // address to the next node int the list
};
//insertion in last 
int insertNode(node **root, node *data)
{
    if(!data) return 0; 
    //check if root available. if not data will be the root
    if(!*root)
    {
        *root = data;
        data->next = NULL;
        return 1;
    }
    node *temp = *root;
    while(temp->next)
    {
        temp = temp->next;
    }
    temp->next = data;

    return 1;
}
//deletion. if multiple ocures first will delete
int deleteNode(node **root, int num)
{
    if(!root) return 0;
    node *temp = *root;
    if(temp->data == num)
    {
        if(temp->next)
        {
            *root = temp->next;
        }
        free(temp);
        return 1;
    }
    while(temp->next)
    {
        node* deleteThis = temp->next;
        if((temp->next->next) && temp->next->data == num)
        {
            //delete next            
            temp->next = temp->next->next;
            free(deleteThis);
            return 1;
        }
        else if(temp->next->data == num)
        {
            //the last node
            temp->next = NULL;
            free(deleteThis);
            return 1;
        }
        temp = temp->next;            
    }
    return 0;
}
int main(int argc, char *argv[]){
    //make root node 
    node* root = NULL;
    // if the input is more than one, error
    if (argc != 2) return EXIT_FAILURE;

    // argv[1] will contain the file name input. 

    FILE *file = fopen(argv[1], "r");

    // we need to make sure the file is not empty, error case. 

    if (file == NULL)
    {
        printf("error\n");
        exit(0);
    }
    // if the file is empty, print an empty line.

    /*int size = ftell(file);
    if (size == 0){
        printf("\n");
    }
    printf("%d",size);*/

    char linestring[BUFSIZ]; // this will hold our file input after we read/scan it.

    // now we need to read the file

    while (fgets(linestring, sizeof(linestring), file)) // reads the entire file until it hits Null
    {
       // printf("%s", linestring); // stores each line into linestring. 


         // need to split up the lines so that we separate letters from integars. 
         // look for \t or " " and use strtok. 

        char* letter = strtok(linestring, " \t\n"); // stores the letter from the line
        char* number = strtok(NULL, " \t\n"); // stores the numbers from the line
        //changed from here
        int n =atoi(number);//convert string to int
        if(*letter == 'i')
        {
            //creates node to be inserted 
            node *data = NULL;
            data  = (struct node*)malloc(sizeof(node));            
            data->data = n; 
            data->next = NULL;

            if(!insertNode(&root,data)) //if insertion gose wrong 0 will return
                printf("insert %s was faild\n", number); 
        }
        else
        {
            if(!deleteNode(&root,n))
                printf("Couldnt Delete (%s not in the list)\n )", number); 
        }
    }
}

答案 1 :(得分:-1)

**FULL WORKING CODE IN C++ FOR REFERENCE**

#include<bits/stdc++.h>
#include<fstream>
using namespace std;

struct node{
int data;
struct node* next;
};

typedef struct node node;
typedef struct node* nodeptr;

nodeptr Create(nodeptr head,nodeptr &current,int x){

nodeptr temp = (nodeptr)malloc(sizeof(node));
temp->next=NULL;
temp->data=x;

if(current==NULL){
current=temp;
head=temp;
}

else{
current->next=temp;
current=temp;
}

return head;
}

nodeptr Delete(nodeptr head, int x){

nodeptr temp = head;
nodeptr temp1;

while(temp->data!=x){
    temp1=temp;
    temp=temp->next;
}

temp1->next=temp->next;
free(temp);
temp=NULL;

return head;
}

void PrintC(nodeptr head){

nodeptr temp = head;

while(temp!=NULL){
    cout<<temp->data<<" ";
    temp=temp->next;
}

}

int main(){

nodeptr current = NULL;
nodeptr head = current;

string str;

ofstream myfile;
myfile.open("file.txt",ios::out);
myfile << "i 1 i 2 i 3 i 4 i 5 d 2 d 3";
myfile.close();

ifstream myfile1("file.txt");
getline(myfile1,str);
myfile1.close();

int i=0;

while(str[i]!='\0'){

    if(str[i]!=' '){

           if(str[i]=='i'){

                int n = str[i+2]-48;
                head=Create(head,current,n);
                i=i+2;
           }

            else if(str[i]=='d'){

                int n = str[i+2]-48;
                head = Delete(head,n);
                i=i+2;
            }

    }

i++;
}

cout<<endl;

PrintC(head);

return 0;
}