PDA

View Full Version : URGENTE: lista concatenata in C


alexsimone
31-01-2007, 11:01
Ciao a tutti ragazzi, è un bel po' che sto sbattendo per la realizzazione di una lista in C. Innanzitutto nn ho ben capito come funziona e vi sarei grato se qualkuno potesse postarmi un codice esempio, ma cmq anche copiando il codice di una lista che viene proposta sul libro Deitel e Deitel C Corso di prog. il mio compilatore da errore. Uso dev c versione 4 e vi posto il codice:

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

struct listNode{
char data;
struct listNode *nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;


void insert(ListNodePtr*sPtr, char value);
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr);
void instructions(void);

int main()
{
ListNodePtr startPtr=NULL;
int choice;
char item;

instructions();
printf("? ");
scanf("%d",&choice);

while(choice!=3){
switch(choice){

case 1:
printf("Enter a characther:");
scanf("\n%c",&item);
insert(&startPtr,item);
printList(startPtr);
break;



default:
printf("invalid choice.\n\n");
instructions();
break;
}
printf("?");
scanf("%d", &choice);
}

printf("End of run.\n");
return 0;
}


void instructions(void)
{
printf("Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n");
}

void insert(ListNodePtr *sPtr, char value)
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;

newPtr=malloc(sizeof(ListNode));

if(newPtr!=NULL){
newPtr->data=value;
newPtr->nextPtr=NULL;

previousPtr=NULL;
currentPtr=*sPtr;

while(currentPtr!=NULL && value>currentPtr->data){
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if(previousPtr==NULL){
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
}
else{
printf("%c not inserted.No memory available.\n",value);
}
}



void printList(ListNodePtr currentPtr);
{
if(currentPtr==NULL){
printf("List is Empty.\n\n");
}
else{
printf("The list is:\n");

while (currentPtr!=NULL){
printf("%c-->",currentPtr->data);
currentPtr=currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}


mi da i seguenti errori:

Compilatore: Default compiler
Esecuzione di g++.exe...
g++.exe "D:\Uniroma\Programmazione 1\lista.cpp" -o "D:\Uniroma\Programmazione 1\lista.exe" -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
D:/Uniroma/Programmazione 1/lista.cpp: In function `void insert(ListNode**,
char)':
D:/Uniroma/Programmazione 1/lista.cpp:68: invalid conversion from `void*' to `
ListNode*'

D:/Uniroma/Programmazione 1/lista.cpp: At global scope:
D:/Uniroma/Programmazione 1/lista.cpp:99: parse error before `{' token
D:/Uniroma/Programmazione 1/lista.cpp:108: ISO C++ forbids declaration of `
currentPtr' with no type
D:/Uniroma/Programmazione 1/lista.cpp:108: base operand of `->' is not a
pointer
D:/Uniroma/Programmazione 1/lista.cpp:109: parse error before `}' token
D:/Uniroma/Programmazione 1/lista.cpp:110: ISO C++ forbids declaration of `
printf' with no type
D:/Uniroma/Programmazione 1/lista.cpp:110: `int printf' redeclared as different
kind of symbol
C:/Dev-Cpp/include/stdio.h:213: previous declaration of `int printf(const
char*, ...)'
D:/Uniroma/Programmazione 1/lista.cpp:110: invalid conversion from `const char*
' to `int'

D:/Uniroma/Programmazione 1/lista.cpp:111: parse error before `}' token

Esecuzione terminata

yorkeiser
31-01-2007, 11:21
Ricontrolla quello che hai copiato, vedo diversi errori che non credo tu possa trovare stampati su un libro

alexsimone
31-01-2007, 11:31
Si , c'era un = in meno in un controllo nella funzione printList ma ora ho corretto e cmq mi da sempre errore

yorkeiser
31-01-2007, 12:24
void printList(ListNodePtr currentPtr);
{
....

Togli il punto e virgola dopo il prototipo

Il resto è giusto e il programma parte, perlomeno compilato col cl Microzozz

alexsimone
31-01-2007, 13:51
mi da sempre errore :muro:

Compilatore: Default compiler
Esecuzione di g++.exe...
g++.exe "D:\Uniroma\Programmazione 1\lista.cpp" -o "D:\Uniroma\Programmazione 1\lista.exe" -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
D:/Uniroma/Programmazione 1/lista.cpp: In function `void insert(ListNode**,
char)':
D:/Uniroma/Programmazione 1/lista.cpp:68: invalid conversion from `void*' to `
ListNode*'

Esecuzione terminata

yorkeiser
31-01-2007, 15:14
Uhm, il cl non lo segnala, comunque suppongo che l'errore sia nel mancato cast sulla malloc


newPtr=(ListNode*)malloc(sizeof(ListNode));

alexsimone
31-01-2007, 15:27
si ora funziona grazie del tuo prezioso aiuto