P
Pete
In the following source I need to enter a string into a list. I'm using
dataitem[MAX] and data[MAX] for this, upon exit input string should be
displayed before being free()'ed. Since changing the code from an char
dataitem to dataitem[] I'm getting the error
left operand must be l-value
on the 2 lines in the AddItem() function, the lines are - listpointer ->
dataitem = data; 2 times. The changes I made are the [] in prototypes and
in
main() char data[MAX] and #define MAX 10. Your help is very much
appreciated.
/*Code to insert into nth position in linked list*/
#include <stdio.h>
#include <stdlib.h>
#include "ctype.h"
#define FALSE 0
#define MAX 10
typedef struct listelement { /*structure listelement*/
int position; /*variable to hold postion in string*/
char dataitem[MAX]; /*pointer to char*/
struct listelement *next; /*pointer to next struct item*/
} listelement;
/*Prototypes*/
void Menu(int *choice); /* Menu function */
listelement *AddItem (listelement *listpointer, int posn, char data[]);
/* AddItem function */
void ClearQueue(listelement *listpointer); /* ClearQueue function */
listelement *RemoveItem (listelement *listpointer); /* RemoveItem
function */
/********************main**********************/
main () {
listelement *listpointer;
char data[MAX]; /*variable to take data element*/
int posn, choice; /*variables to take posn & Menu choice*/
listpointer = NULL; /*1st element in list*/
do {
Menu(&choice);
switch(choice) { /*switch procedure for Menu*/
case 1:
fflush(stdin);
printf("Enter data item to add to list\n");
scanf("%s", &data);
printf("enter position in list\n");
scanf("%d", &posn);
listpointer = AddItem(listpointer, posn, data); /* call AddItem
function */
case 2:
break;
}
}while(choice !=2);
ClearQueue(listpointer); /* clear the queue */
} /* End main !!!*/
/*********************Menu*********************/
void Menu(int *choice) {
char local; /* local variable */
printf("\nEnter\t1 to add item,\n\t2 to quit\n");
do {
local = getchar();
if((isdigit (local) == FALSE) && (local != '\n')) {
printf("\nyou must enter an integer.\n");
printf("Enter 1 to add, 2 to quit\n");
}
} while(isdigit((unsigned char) local) == FALSE);
*choice = (int) local - '0';
}
/******************AddItem********************/
listelement *AddItem (listelement *listpointer, int posn, char data[])
{
listelement *lp = listpointer;
if (listpointer != NULL) {
while (listpointer -> next);
listpointer -> next = (struct listelement *) malloc(sizeof(listelement));
listpointer = listpointer -> next;
listpointer -> next = NULL;
listpointer -> dataitem = data;
listpointer -> position = posn;
return lp;
}
else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
listpointer -> next = NULL;
listpointer -> dataitem = data;
listpointer -> position = posn;
return listpointer;
}
}
/*****************ClearQueue*********************/
void ClearQueue(listelement *listpointer) {
while (listpointer != NULL) {
listpointer = RemoveItem (listpointer);
}
}
/*****************RemoveItem**********************/
listelement *RemoveItem (listelement *listpointer) {
listelement *tempp; /* temporary */
printf ("Element removed is %s\n", listpointer -> dataitem);
printf ("Position of element removed is %d\n", listpointer -> position);
tempp = listpointer -> next;
free (listpointer);
return tempp;
}
dataitem[MAX] and data[MAX] for this, upon exit input string should be
displayed before being free()'ed. Since changing the code from an char
dataitem to dataitem[] I'm getting the error
left operand must be l-value
on the 2 lines in the AddItem() function, the lines are - listpointer ->
dataitem = data; 2 times. The changes I made are the [] in prototypes and
in
main() char data[MAX] and #define MAX 10. Your help is very much
appreciated.
/*Code to insert into nth position in linked list*/
#include <stdio.h>
#include <stdlib.h>
#include "ctype.h"
#define FALSE 0
#define MAX 10
typedef struct listelement { /*structure listelement*/
int position; /*variable to hold postion in string*/
char dataitem[MAX]; /*pointer to char*/
struct listelement *next; /*pointer to next struct item*/
} listelement;
/*Prototypes*/
void Menu(int *choice); /* Menu function */
listelement *AddItem (listelement *listpointer, int posn, char data[]);
/* AddItem function */
void ClearQueue(listelement *listpointer); /* ClearQueue function */
listelement *RemoveItem (listelement *listpointer); /* RemoveItem
function */
/********************main**********************/
main () {
listelement *listpointer;
char data[MAX]; /*variable to take data element*/
int posn, choice; /*variables to take posn & Menu choice*/
listpointer = NULL; /*1st element in list*/
do {
Menu(&choice);
switch(choice) { /*switch procedure for Menu*/
case 1:
fflush(stdin);
printf("Enter data item to add to list\n");
scanf("%s", &data);
printf("enter position in list\n");
scanf("%d", &posn);
listpointer = AddItem(listpointer, posn, data); /* call AddItem
function */
case 2:
break;
}
}while(choice !=2);
ClearQueue(listpointer); /* clear the queue */
} /* End main !!!*/
/*********************Menu*********************/
void Menu(int *choice) {
char local; /* local variable */
printf("\nEnter\t1 to add item,\n\t2 to quit\n");
do {
local = getchar();
if((isdigit (local) == FALSE) && (local != '\n')) {
printf("\nyou must enter an integer.\n");
printf("Enter 1 to add, 2 to quit\n");
}
} while(isdigit((unsigned char) local) == FALSE);
*choice = (int) local - '0';
}
/******************AddItem********************/
listelement *AddItem (listelement *listpointer, int posn, char data[])
{
listelement *lp = listpointer;
if (listpointer != NULL) {
while (listpointer -> next);
listpointer -> next = (struct listelement *) malloc(sizeof(listelement));
listpointer = listpointer -> next;
listpointer -> next = NULL;
listpointer -> dataitem = data;
listpointer -> position = posn;
return lp;
}
else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
listpointer -> next = NULL;
listpointer -> dataitem = data;
listpointer -> position = posn;
return listpointer;
}
}
/*****************ClearQueue*********************/
void ClearQueue(listelement *listpointer) {
while (listpointer != NULL) {
listpointer = RemoveItem (listpointer);
}
}
/*****************RemoveItem**********************/
listelement *RemoveItem (listelement *listpointer) {
listelement *tempp; /* temporary */
printf ("Element removed is %s\n", listpointer -> dataitem);
printf ("Position of element removed is %d\n", listpointer -> position);
tempp = listpointer -> next;
free (listpointer);
return tempp;
}