bigheadpirate07
Programmer
So I tried to finish this LAST week so that I wouln't have to be doing this now, but it's funny how things work sometimes. I am new to C, and this is the first semester I have taken any course with C in it. This assignment is to write a Linked List in C. Pretty basic, huh? Not when I get a fat segmentation fault (core dumped) yelling at me. I have allocated enough memory and everything, but I cannot figure out what is wrong for the life of me. If anyone can help I would do a backflip or something.
This program is breaking my balls,
David
p.s. If this is readable, I've narrowed the problems down to lines 89 and 115. And by the way, there is a whole lot of retarded, crappy, bad convention stuff in here that I'm required to use, unfortunately.
This program is breaking my balls,
David
p.s. If this is readable, I've narrowed the problems down to lines 89 and 115. And by the way, there is a whole lot of retarded, crappy, bad convention stuff in here that I'm required to use, unfortunately.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#define STRLEN 20
typedef struct
{
char *str; /* str is a dynamic array of characters*/
int len; /* number of characters */
} String;
typedef struct listNode
{
String data;
struct listNode *link;
} listNode;
typedef struct
{
listNode *head;
} List;
enum {true, false};
typedef int bool;
//typedef enum {false, true} bool;
void menu(List *x[]);
void menuPrompt(void);
void menuSelect(char Option, List *x[]);
void initialize(List *x);
void print(List *x);
List *deleteString(List *x);
List *addString(List *x);
void ListAddNode(List *L, String Item, int *Success);
bool search(List *x);
List *deepCopy(List *x);
List *concatenate(List *x, List *y);
void destroyList(List *x);
void FindSlot(List *L, String Item, listNode **Prev);
void ListDeleteNode(List *L, String Target, int *Success);
void FindPointersToNode(List *L, String Target, listNode **P, listNode **Prev, int *Success);
void ListFindNode(List *L, String Target, String *Item, int *Success);
int arrayLength;
int main()
{
int numLists=-1;
int m=0;
String temp;
bool read = false;
char *fileName = malloc(20*(sizeof(char)));
printf("Enter the number of Lists to be used: ");
scanf("%d",&numLists);
List *listArray[numLists];
arrayLength = numLists;
for(m=0;m<numLists;m++)
{
read = true;
List *tempList = calloc(1, sizeof(List));
//Get filename
printf("Enter a filename to be read: ");
scanf("%s", fileName);
FILE *f;
if ( (f = fopen(fileName, "r")) == 0 ) {
printf("You are stupid! put the right file in!\n");
return 1;
}
while ( (read == true) && (!feof(f)))
{
temp.str = calloc( STRLEN, sizeof(char) );
//read one line at a time and add each line to a new node
fgets(temp.str, STRLEN, f);
printf("read in: %s", temp.str);
ListAddNode(tempList, temp, &read);
}
listArray[m] = tempList;
//free(tempList);
tempList=NULL;
fclose(f);
printf("Done Reading File.\n");
}
printf("ertu\n");
menu(listArray);
return 0;
}
void menu(List *x[])
{
printf("fghj\n");
listNode *p = x[0]->head;
printf("bvvbm\n");
printf("%s\n",p->data.str);
char choice;// = ' ';
char choice2;
while (choice != '9')
{
scanf("%c", &choice2);
menuPrompt();
printf("choice: ");
scanf("%c",&choice);
printf("still going");
menuSelect(choice, x);
}
}
void menuPrompt()
{
printf("****************************************\n");
printf("****************************************\n");
printf("** **\n");
printf("** Welcome to Manage Your Lists! **\n");
printf("** **\n");
printf("** **\n");
printf("** 1) Initialize a new List **\n");
printf("** 2) Print the List **\n");
printf("** 3) Remove a String **\n");
printf("** 4) Add a String **\n");
printf("** 5) Search for a String **\n");
printf("** 6) Make a Deep Copy of the List **\n");
printf("** 7) Concatenate 2 Lists **\n");
printf("** 8) Destroy the List **\n");
printf("** 9) Exit **\n");
printf("** **\n");
printf("** **\n");
printf("** Please Make Your Selection **\n");
printf("** **\n");
printf("****************************************\n");
printf("****************************************\n");
}
void menuSelect (char Option, List *x[])
{
printf("asdf");
int okay = 0;
int num = -1;
while(okay == 0)
{
printf("Which List do you want to use? (enter an integer from 0 to %s): ", arrayLength);
scanf("%d",&num);
if(num>=0&&num<arrayLength)
okay=1;
}
printf("ok so far");
switch (Option)
{
case '1': initialize(x[num]);
break;
case '2': print(x[num]);
break;
case '3': x[num]=deleteString(x[num]);
break;
case '4': x[num]=addString(x[num]);
break;
case '5': search(x[num]);
break;
case '6': {
int index = arrayLength+1;
List *newList[arrayLength+1];// = malloc(sizeof(List)*(arrayLength+1));
newList[arrayLength] = x[num]=deepCopy(x[num]);
x = newList;
printf("The deep copy is stored in the array at index %d.\n", index);
}
break;
case '7' : {
int okay2 = 0;
int num7 = -1;
int index = arrayLength+1;
List *newList[arrayLength+1];// = malloc(sizeof(List)*(arrayLength+1));
while(okay2 == 0)
{
printf("Which other List do you want to concatenate? (enter an integer from 0 to %s): ", arrayLength);
scanf("%d",&num7);
if(num7>=0&&num7<arrayLength)
okay2=1;
}
newList[arrayLength] = concatenate(x[num], x[num7]);
x = newList;
printf("The concatenation is stored in the array at index %d.\n", index);
}
break;
case '8': destroyList(x[num]);
break;
case '9': ;
break;
default: printf("Invalid Choice. Press <Enter> and try again.\n");
char test = getchar();
}
}
void ListAddNode(List *L, String Item, int *Success)
{
listNode *P, *Prev;
P = (listNode *) malloc(sizeof(listNode));
/*if (P == NULL)
{ //no sufficient memory
*Success = false;
return;
}*/
P->data = Item;
*Success = true;
FindSlot(L, Item, &Prev);
if (Prev == NULL)
{ //insert at front of list L
P->link = L->head;
L->head = P;
}
else{
P->link = Prev->link;
Prev->link = P;
}
//return *L;
}
void initialize(List *x)
{
char *strings;
int flag=1;
char *tempString;
char *tempString2;
String temp2;
String temp3;
printf("Enter strings to be added to a list:\n");
scanf("%s", strings);
tempString = strtok(strings, " ");
strcpy(temp2.str,tempString);
temp2.len=strlen(tempString);
ListAddNode(x, temp2, 0);
while(flag!=0)
{
if(strtok(NULL, " ")!=NULL){
tempString2 = strtok(NULL, " ,.");
strcpy(temp3.str,tempString2);
temp3.len=strlen(tempString2);
ListAddNode(x, temp3, 0);}
else
flag=0;
}
//strtok
//tokenize it
//assign the substrings to an array
//add each element of the array to the list
//x[num]->head = NULL;
}
void print(List *x)
{
int flag = 0;
//FindPointersToNode(&x, searchString, **P, **Prev, 0);
listNode *p=x->head;
//printf("%s",x->head);
while(p!=NULL)
{
if(p->link!=NULL)
{
printf("%s ",p->data.str);
p=p->link;
}
else;
}
//printf("%s",
//go through each node and print the contents
}
List *deleteString(List *x)
{
String temp;
printf("Enter a string to remove: ");
scanf("%s",temp.str);
ListDeleteNode(x, temp, 0);
/*
//search(temp);
listNode p;
//p.data=temp;
//find where the string to be deleted is
*x[num].link=p.link;
p.link=NULL;
//List z = malloc(sizeof(List));*/
return x;
}
void ListDeleteNode(List *L, String Target, int *Success)
{
listNode *P, *Prev;
FindPointersToNode(L, Target, &P, &Prev, Success);
if (*Success) {
if (Prev == NULL) // remove node at front of L
L->head = L->head->link;
else
Prev->link = P->link;
}
free(P);
}
List *addString(List *x)
{
listNode *p, *Prev;
String temp;
printf("Enter a string to add: ");
scanf("%s",temp.str);
ListAddNode(x, temp, 0);
return x;
}
bool search(List *x)
{
String searchString;
String *Item;
int *Success=0;
printf("Enter a string to search for: ");
scanf("%s",searchString);
ListFindNode(x, searchString, Item, Success);
//FindPointersToNode(x, searchString, x->head, **Prev, 0);
if(*Success!=0)
printf("%s found",searchString);
else printf("%s not found", searchString);
/*int flag = 0;
while(flag!=1)
{
if(x->link!=null)
{
if(strcmp(x->data,searchName)==0)
printf("%s ",x->data);
}
else flag=1;
}*/
//FindPointersToNode(List *L, String Target, listNode **P, listNode **Prev, int *Success)
//if(strcmp(x[i]->lastName,searchName)==0)
}
List *deepCopy(List *x)
{
List *z;
//s2.str = (char *)malloc(sizeof(char) * 20);
listNode *p=x->head;
while(p!=NULL)
{
if(p->link!=NULL)
{
ListAddNode(z, p->data, 0);
p=p->link;
}
else;
}
//strcpy(s2.str,p=p->data.str)
return z;
}
List *concatenate(List *x, List *y)
{
//concatenate lists x and y
List *z;
listNode *p=x->head;
listNode *q=x->head;
while(p!=NULL)
{
if(p->link!=NULL)
{
ListAddNode(z,p->data,0);
p=p->link;
}
else;
}
while(q!=NULL)
{
if(q->link!=NULL)
{
ListAddNode(z,q->data,0);
q=q->link;
}
else;
}
return z;
}
void destroyList(List *x)
{
free(x);
x->head=NULL;
//destroy it, using free
}
void FindSlot(List *L, String Item, listNode **Prev)
{
listNode *Q;
*Prev = NULL;
Q = L->head;
while( (Q != NULL) && strcmp(Item.str,Q->data.str)!=0)//(Item < Q->data) != true )
{
*Prev = Q;
Q = Q->link;
}
}
void ListFindNode(List *L, String Target, String *Item, int *Success)
{
listNode *P, *Prev;
FindPointersToNode(L, Target, &P, &Prev, Success);
if (*Success) *Item = P->data;
}
// update a node corresponding to Target
/*
void ListUpdateNode(List *L, ListData Target, ListData NewData,int *Success)
{
listNode *P, *Prev;
FindPointersToNode(L, Target, &P, &Prev, Success);
if (*Success) P->Data = NewData;
}
*/
void FindPointersToNode(List *L, String Target, listNode **P, listNode **Prev, int *Success)
{
*Prev = NULL;
*P = L->head;
*Success = 0;
while(*P != NULL && ! *Success)
{
if ( strcmp(Target.str, (*P)->data.str)==0)
*Success = 1;
else
{
*Prev = *P;
*P = (*P)->link;
}
}
}