2014-10-22 2 views
0

Я пытался исправить несколько ошибок, которые я получаю при компиляции этой программы, но не могу найти никаких решений. Это следующие ошибки, которые происходят после фактического кода. Большинство ошибок являются «унальными» проблемами, хотя я думал, что это правильный способ его использования. Если бы кто-нибудь мог указать мне в правильном направлении, было бы очень полезно.Невозможно скомпилировать программу Linked List

testReadLinkedList.c:13: warning: no semicolon at end of struct or union 
testReadLinkedList.c: In function ‘insertListElement’: 
testReadLinkedList.c:31: error: incompatible types in assignment 
testReadLinkedList.c: In function ‘readFile’: 
testReadLinkedList.c:43: warning: comparison between pointer and integer 
testReadLinkedList.c: In function ‘countList’: 
testReadLinkedList.c:51: error: invalid initializer 
testReadLinkedList.c:54: error: invalid operands to binary != 
testReadLinkedList.c:56: error: invalid type argument of ‘unary *’ 
testReadLinkedList.c: In function ‘printList’: 
testReadLinkedList.c:65: error: invalid initializer 
testReadLinkedList.c:70: error: invalid type argument of ‘unary *’ 
testReadLinkedList.c:71: error: invalid type argument of ‘unary *’ 
testReadLinkedList.c:72: error: invalid type argument of ‘unary *’ 
testReadLinkedList.c:73: error: invalid type argument of ‘unary *’ 
testReadLinkedList.c:81: error: invalid type argument of ‘unary *’ 


#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <math.h> 
#include <float.h> 

typedef struct LinkedListNode { 
    int red; 
    int green; 
    int blue; 
    char function[128]; 
    struct LinkedListNode* next 
} LinkedListNode; 

typedef struct { 
    LinkedListNode* head; 
} Linkedlist; 

void initLinkedList(Linkedlist* listIn) { 

    listIn = (Linkedlist*)malloc(sizeof(Linkedlist)); 
    (*listIn).head = NULL; 
} 

void insertListElement(Linkedlist* listIn, int redIn, int greenIn, int blueIn, char* functionIn) { 
    LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode)); 

    (*newNode).red = redIn; 
    (*newNode).green = greenIn; 
    (*newNode).blue = blueIn; 
    (*newNode).function = functionIn; 

    (*newNode).next = (*listIn).head; 
    (*listIn).head = newNode; 
} 

void readFile(FILE* fileIn, Linkedlist* listIn) { 

    char tempLine[128]; 
    int tempRed, tempGreen, tempBlue; 
    char tempFunction[128]; 

    while(fgets(tempLine, 128, fileIn) != EOF) { 
     sscanf(tempLine, "%d %d %d %[^\n]", &tempRed, &tempGreen, &tempBlue, tempFunction); 
     insertListElement(listIn, tempRed, tempGreen, tempBlue, tempFunction); 
    } 

} 

int countList(Linkedlist* listIn) { 
    LinkedListNode currNode = (*listIn).head; 
    int size = 0; 

    while(currNode != NULL) { 
     size++; 
     currNode = (*currNode).next; 
    } 
    return size; 
} 

void printList(Linkedlist* listIn) { 

    int i, tempRed, tempGreen, tempBlue; 
    char tempFunction[128]; 
    LinkedListNode currNode = (*listIn).head; 
    int listSize = countList(listIn); 

    for(i = 0;i < listSize; i++) { 

     tempRed = (*currNode).red; 
     tempGreen = (*currNode).green; 
     tempBlue = (*currNode).blue; 
     tempFunction = (*currNode).function; 

     printf("Red: %d\n",tempRed); 
     printf("Green: %d\n",tempGreen); 
     printf("Green: %d\n",tempBlue); 
     printf("Function: %s\n",tempFunction); 
     print("t\n"); 

     currNode = (*currNode).next; 
    } 

} 

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

    FILE* f1; 
    Linkedlist* list1; 

    f1 = fopen(argv[1], "r"); 
    if(f1 == NULL) { 
     printf("Error: could not open file"); 
    } else { 
     initLinkedList(list1); 
     readFile(f1, list1);   
    } 

} 
+0

'testReadLinkedList.c: 13: предупреждение: не точку с запятой в конце структуры или объединения ' –

ответ

1
  1. struct LinkedListNode* next -> отсутствует точка с запятой.

  2. (*newNode).function = functionIn; изменить на strcpy(newNode->function,functionIn).

  3. char * fgets (char * str, int num, FILE * stream); если не удалось (если конец файла встречается при попытке прочитать символ), он вернет NULL (не EOF).

несколько советов. Есть много других ошибок. Возможно, вам захочется снова ознакомиться с базовыми концепциями C-указателей.

3

Здесь:

(*newNode).function = functionIn; 

вам нужно strcpy functionIn к (*newNode).function, вы не можете просто присвоить ему. Кроме того, на этой линии:

while(fgets(tempLine, 128, fileIn) != EOF) 

fgets не возвращает EOF, но NULL в случае ошибки, так что вам нужно сделать:

while (fgets(tempLine, 128, fileIn)) 

и на этой линии:

LinkedListNode currNode = (*listIn).head; 

head является указатель, вам необходимо исправить тип currNone:

LinkedListNode *currNode = listIn->head; 

Наконец, вам нужно изменить функцию инициализации:

void initLinkedList(Linkedlist **listIn) { 
    *listIn = (Linkedlist *)malloc(sizeof(Linkedlist)); 
    (*listIn)->head = NULL; 
} 

и призыв:

initLinkedList(&list1); 
0
#include<iostream> 
#include<conio.h> 
#include<string.h> 
using namespace std; 
struct node 
{ 
    int itemno; 
    node *next; 
}; 
node *temp=NULL,*top=NULL; 
int e; 

void push() 
{ 
    temp=new node; 
    temp->next=NULL; 
    if(temp==NULL) 
    { 

    cout<<"stack overflow"; 
    return; 
} 

    cout<<"enter the itemno"<<endl; 
    cin>>temp->itemno; 
    //temp->itemno=e; 
    if(top==NULL) 
    { 
     top=temp; 
} 
    else 
    { 
     top=temp->next; 
     top=temp; 

    } 
} 
void pop() 
{ 
    if(top==NULL) 

     cout<<"stack underflow"; 


    else 
    { 
     top=temp; 
      cout<<temp->itemno; 
     top=top->next; 


    } 
     delete temp; 
} 
void display() 
{ 
    if(top==NULL) 
    { 

    cout<<"stack underflow"; 
    return; 
} 
    else 
    { 
     temp=top; 
     while(temp!=NULL) 
     { 
      cout<<temp->itemno; 
      cout<<endl; 
      temp=temp->next; 
     } 
    } 
} 
int main() 
{ char choice1; 
    int choice; 
    while(1) 
    { 
     cout<<"menu"<<endl; 
     cout<<"1.push"<<endl; 
     cout<<"2.pop"<<endl; 
     cout<<"e.displAY"<<endl; 
     cout<<"4.exit"<<endl; 
     cout<<"enter your choice"<<endl; 
     cin>>choice; 
     switch(choice) 
     { 
      case 1:{ 
       do 
       { push(); 
       cout<<"enter y to add or n to cancel"<<endl; 
       cin>>choice1; 


       }while(choice1=='y'); 


      break;} 
      case 2:{ 
       do 
       { 
        cout<<"enter y to delete or n to cancel"<<endl; 
        cin>>choice1; 
        if(choice1=='y') 
        pop(); 
       }while(choice1=='y'); 


      break;} 
      case 3:display(); 
      break; 
      case 4:return 0; 
      default:break; 

     } 

    } 
     getch(); 
     return 0; 


} can anybody point out the mistake in this.