Cannot access memory at address at address "" (gdb) - c

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct Node {
/* Data fields with appropraiate types */
char fname[64];
char lname[64];
char puid[16];
int Age;
struct Node *next;
};
struct List {
struct Node *start;
int numNodes;
};
struct List * initialize_list() {
struct List *list = (struct List *) malloc(sizeof(struct List *));
list -> numNodes = 0;
list -> start = (struct Node *) malloc(sizeof (struct Node *));
return list;
}
struct List * CreateList() {
struct List *list = initialize_list();
return list;
}
void Traverse(struct List *list) {
struct Node *node = (struct Node *) malloc (sizeof(struct Node));
int i = 1;
node = list -> start -> next;
while (node != NULL) {
printf ("\nNode: %d", i);
printf("\nPUID: %s", node -> puid);
node = node -> next;
i++;
}
if (i == 1) {
printf("\n\nEmpty List");
}
}
struct Node *CreateNode(char first_name[], char last_name[], char PUID[], int age) {
struct Node *newNode = (struct Node *) malloc (sizeof(struct Node *));
strcpy(newNode->fname, first_name);
strcpy(newNode->lname, last_name);
strcpy(newNode->puid, PUID);
newNode->Age = age;
newNode->next = NULL;
return newNode;
}
void InsertFront(struct List *list, char first_name[], char last_name[], char PUID[], int age) {
struct Node *newNode = CreateNode (first_name, last_name, PUID, age);
if (list -> numNodes == 0) {
list -> start -> next = newNode;
list -> numNodes++;
return;
}
newNode -> next = list -> start -> next;
list -> start -> next = newNode -> next;
list -> numNodes++;
return;
}
int main () {
struct List *myList = CreateList();
while (1) {
int option = 0;
char fname[64];
char lname[64];
char puid[16];
int age;
printf("\n0. Exit Program \n1. Insert Front\n2. Insert Middle\n3. Insert End\n4. Delete Front\n5. Delete Middle\n6. Delete End\n7. Traverse \n8. Look Up by Index\n");
printf ("Enter option: ");
option = getchar();
if (option == '0') {
exit (0);
}
else if (option == '1' || option == '2' || option == '3') {
printf("Enter first name: ");
scanf("%s", fname);
printf("Enter last name: ");
scanf("%s", lname);
printf("Enter PUID: ");
scanf("%s", puid);
printf("Enter age: ");
scanf("%d", &age);
if (option == '1') {
InsertFront (myList, fname, lname, puid, age);
}
else if (option == '2') {
int index;
printf ("Enter position to Insert: ");
scanf ("%d", &index);
InsertMiddle (myList, index, fname, lname, puid, age);
}
else if (option == '3') {
InsertEnd (myList, fname, lname, puid, age);
}
}
else if (option == 4) {
}
else if (option == 5) {
}
else if (option == 6) {
}
else if (option == '7') {
Traverse (myList);
}
else if (option == 8) {
}
else {
}
getchar();
}
return 0;
}
I'm relearning some of this but I'm not sure where I'm going wrong.
I get a segmentation fault when the program reaches the Traverse() function.
I can access the Node that before the iteration of the while loop is not complete. As soon as the next iteration begins the myList -> start -> next can't be accessed any longer.

In Create node method:
malloc (sizeof(struct Node *))
Will allocate only 4 or 8 bytes because it sizeof pointer.
It should be:
malloc (sizeof( Node ))
to allocate space for an object

Related

Reversing an array of linked list recursively isn't reversing all nodes in order

I have an array of linked list which I am trying to reverse recursively. When I call the function to reverse it will not reverse all of the nodes but rather a couple of the nodes.
The reverse function appears to be deleting the first node (base case) and filling its spot with the last node (end of sub case). I think that the problem lies in the calling of the for loop within the reverse_nodes function however that doesn't seem to fix it.
Here is some output..
pre-reverse function:
-----
group 0
alice, 2
-----
group 1
martin, 4
-----
group 2
keanu, 6
-----
group 3
miles, 8
post - reverse function
-----
group 0
miles, 8
-----
group 1
martin, 4
-----
group 2
keanu, 6
-----
group 3
miles, 8
I am trying to get it to reverse to that it reads: 8,6,4,2
Please note I have only included relevant code blocks such as the struct architecture, the head/tail construction, the deletion of all nodes prior to reading in the binary file, reading binary file to nodes, and the main function. Can I get some help figuring out what in the reverse function is causing it to not completely reverse? Thank for your time. See code below!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
int size;
struct node *next;
}node;
node* head[4]={NULL,NULL,NULL,NULL};
node* tail[4]={NULL,NULL,NULL,NULL};
void wipe_nodes()
{
int i;
node *p=NULL;
for(i=4; i>0; i--)
{
p=head[i];
if(p == NULL)
{
printf("Nodes are clear!\n");
}
while(p != NULL)
{
delete_party(p->name, p->size); // cant call name and size
p = p -> next;
}
}
}
void bin_to_list(char *filename)
{
FILE *fp;
int ret;
fp = fopen(filename, "rb");
if (fp == NULL)
{
printf("Null file!\n");
return;
}
node temp;
//temp = (node *)malloc(sizeof(node));
while((ret = fread(&temp, sizeof(node), 1, fp) > 0))
{
printf("%s %d", temp.name, temp.size);
if(temp.size == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(temp.size >= 1 && temp.size <= 2)
{
add_party(0, temp.name, temp.size);
}
else if(temp.size >= 3 && temp.size <= 4)
{
add_party(1, temp.name, temp.size);
}
else if(temp.size >= 5 && temp.size <= 6)
{
add_party(2, temp.name, temp.size);
}
else if(temp.size >= 7)
{
add_party(3, temp.name, temp.size);
}
}
fclose(fp);
return;
}
void reverse_nodes(node *p, node *q)
{
int i;
for(i=0; i<4; i++)
{
//node *p=head[i];
if(p == NULL)
{
printf("Error, no nodes!\n");
return;
}
if (p->next == NULL)
{
printf("LOL, only one node! Can't reverse!\n");
head[i] = p;
}
else
{
reverse_nodes(p->next, p);
}
p->next = q;
return;
int main(int argc, char *argv[])
{
int x, i;
read_to_list(argv[1]);
bin_to_list(argv[2]);
while (1)
{
fflush(stdin);
printf("\n\nEnter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to change party size.\nEnter 5 to quit (write to .txt file).\nEnter 6 to read from bin file.\nEnter 7 to reverse the list.\n\n");
scanf("%d",&x);
char name[20];
int size;
switch(x)
{
case 1:
printf("\nParty Name: ");
scanf("%s", name);
printf("\nParty Size: ");
scanf("%d", &size);
if(size == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(size >= 1 && size <= 2)
{
add_party(0, name, size);
}
else if(size >= 3 && size <= 4)
{
add_party(1, name, size);
}
else if(size >= 5 && size <= 6)
{
add_party(2, name, size);
}
else if(size >= 7)
{
add_party(3, name, size);
}
break;
case 2:
printf("\nSize of party to delete: ");
scanf("%i", &size);
delete_party(NULL, size);
break;
case 3:
list_parties();
break;
case 4:
change_partysize(name, size);
break;
case 5:
write_to_file(argv[1]);
write_to_bin(argv[2]);
exit(0);
break;
case 6:
wipe_nodes();
bin_to_list(argv[2]);
break;
case 7:
for(i=0; i<4; i++)
{
reverse_nodes(head[i], NULL);
}
break;
default:
continue;
}
}
}
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getchar();
}
Your reverse_nodes function is just setting the next member of the node, not the current node, so when it gets to the end of the list, it will set the next of the last member to the previous member in the linked list but leave the last member untouched.
This worked for me:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
int size;
struct node *next;
}node;
node* head;
void reverse_nodes(node ** pphead)
{
node *prev = NULL;
node *current = *pphead;
node *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*pphead = prev;
}
void main(void)
{
int i;
node * phead;
head = phead = (node *)malloc(sizeof(node));
phead->size = 0;
for (i = 0; i < 4; i++)
{
node * p;
phead->next = (node *)malloc(sizeof(node));
phead = phead->next;
phead->size = i + 1;
phead->next = NULL;
}
reverse_nodes(&head);
phead = head;
while (phead)
{
printf("%d\r\n", phead->size);
phead = phead->next;
}
}
Edit:
Recursion is usually a bad idea as you could end up blowing the stack - it's always best to try and do what you need to do in a single (or multiple if required) function(s) if possible, which in this case is easily possible.

printing the nodes in linked list in c

here is a program which inserting 2 names 2 paths and 2 duration s into linked list (struct of linked lists) , printing them and swapping between them when the duration of the first node is 8.
but when the program printing the nodes its prints from all of the nodes the name and the path of the last node
please help me
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
}*n3;
typedef struct Frame frame_t;
struct Link
{
frame_t *frame;
struct Link *next;
}*n ;
typedef struct Link link_t;
struct Link* insert(struct Link *n3);
void print(struct Link* head, int flag);
void swap(struct Link **head, int value);
#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
int main()
{
struct Link* head = NULL;
int flag = 0;
int num = 0;
char namearray[MAX_NAME_SIZE] = { 0 };
char patharray[MAX_PATH_SIZE] = { 0 };
printf("1\n");
printf("\n");
for (int i = 0; i < 2; i++)
{
printf("Enter a number you want to insert - ");
scanf("%d", &num);
printf("\nEnter the name - ");
scanf("%s", &namearray);
printf("\nEnter the path - ");
scanf("%s", &patharray);
printf("\n");
head = insert(head,num,namearray,patharray);
}
print(head, flag);
swap(&head, 8);
printf("1\n");
system("pause");
return 0;
}
struct Link *insert(struct Link *p, int n, char namearray[MAX_NAME_SIZE], char patharray[MAX_PATH_SIZE]) //insert func
{
struct node *temp;
if (p == NULL) //if the node is empty
{
p = (struct Link *)malloc(sizeof(struct Link)); //gives memory to the node
p->frame = (struct Frame *)malloc(sizeof(struct Frame));
if (p == NULL)
{
printf("Error\n");
}
else
{
printf("The number added to the end of the list\n");
}
p->frame->duration = n; //its runing untill the node item is NULL
p->frame->name = namearray;
p->frame->path = patharray;
p->next = NULL;
}
else
{
p->next = insert(p->next, n , namearray , patharray);/* the while loop replaced by
recursive call */
}
return (p);
}
void print(struct Link* head , int flag)//print func
{
if (head == NULL && flag == 0) //if the node is empty
{
printf("The list is empty\n");
return 1;
}
if (head == NULL && flag != 0) //if the node isnt empty but we are in the NULL (last) item of the node
{
printf("\n");
printf("the nodes of the list printed\n");
return;
}
printf("%d ", head->frame->duration);//prints the currect item
printf("\n");
printf("%s ", head->frame->name);//prints the currect item
printf("\n");
printf("%s ", head->frame->path);//prints the currect item
printf("\n");
print(head->next, ++flag);//calls the func recursevly
return 1;
}
void swap(struct Link **head, int value)
{
while (*head && (*head)->frame->duration != value)
{
head = (*head)->next;
}
if (*head && (*head)->next)
{
struct list *next = (*head)->next->next;
(*head)->next->next = *head;
*head = (*head)->next;
(*head)->next->next = next;
}
}
here is the print :
the print of the program

C: Read Struct into Link List (from File)

I am new to C programming. I have created a student database to enter student details into a linked list (in the form of structure "phbook") and save linked list onto text file. The part I cannot get to work is to read the text file records onto the linked list. The program runs but does not update the linked list when I close the program then select to "LOAD FROM EXTERNAL FILE". Please can someone help me figure what the problem is. I know everything worked until I coded the "readFile" and "insertFull" function. Friends have told me that the global variable "struct phbook *list = NULL;is causing the problem. Please let me know.
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct phbook{
int number;
char name[20];
int mark;
struct part *next;
};
struct phbook *list = NULL;
struct phbook *find_student(int number);
void insertFull(struct phbook* list, int number, char Name[10],int mark);
int main(void)
{
int code;
int opt1;
int courses, i, k, j, counter;
for (;;){
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code){
case 1 : insert();
break;
case 2 : search();
break;
break;
case 3 : print();
break;
case 4 :
saveToFile();
break;
case 5 :
readFile();
break;
default: printf("Illegal code\n");
}
printf("\n");
}
}
struct phbook *find_student(int number)
{
struct phbook *p;
for (p = list; p != NULL && number != p->number; p = p->next);//was sorted
if (p != NULL && number == p->number)
return p;
return NULL;
}
void insert(void)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL){
printf("db full er1.\n");
return;
}
printf("enter student id");
scanf("%d", &new_node->number);
for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
printf("Enter name: ");
scanf("%s", &new_node->name);//readline(new_node->name, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->mark);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
}
void search(void)
{
int number;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &number);
p = find_student(number);
if (p != NULL){
printf("Name: %s\n", p->name);
printf("Marks: %d\n", p->mark);
}
else
printf("student not found.\n");
}
void print(void)
{
struct phbook *p;
printf("Student_Number Student_Name Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->number, p->name, p->mark);
}
void saveToFile()
{
FILE* fp;
fp = fopen ("results.txt","w");
struct phbook* curr = list;//he
while(curr!=NULL)
{
fprintf(fp,"%s\n", list->name);
fprintf(fp,"%d\n",curr->number);
fprintf(fp, "%d\n", curr->mark);
curr = curr->next;
}
fclose(fp);
}
void readFile()
{
FILE* fp;
if (!(fp = fopen ("results.txt", "r")))
printf("File NOT Found");
else{
struct phbook *curr;
struct phbook *prev;
char TempName[10];
int TempNumber;
int TempMark;
int done = 0;
int count = 0;
int success;//dummy
curr = list;//sets it to the head (first nde of ll)
if(list == NULL);
printf("List is null\n");
if(curr == NULL)
printf("List is null\n");
while (curr!=NULL)
{
curr = curr->next;
}
while (done == 0)
{
success = fscanf(fp, "%s", TempName);
if (success == 1)
{
success = fscanf(fp, "%d", &TempNumber);
if (success == 1)
{
success = fscanf(fp, "%d", &TempMark);
if(success == 1)
{
insertFull(list,TempNumber,TempName,TempMark);
}
}
}
else
{
done = 1;
}
}
}
}
void insertFull(struct phbook* list, int number, char Name[10],int mark)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL){
printf("db full er1.\n");
return;
}
for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
new_node->number = number;
new_node->mark = mark;
strcpy(new_node->name,Name);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
}
In the procedure
void insertFull(struct phbook* list, int number, char Name[10],int mark)
you are changing your local copy of list, this has no effect on the global phone book list.
You can remove list from the argument lists, thus changing the global variable:
void insertFull(int number, char Name[10],int mark)
this is not strictly good programming, but it is in line with the rest of your code.

how to copy txt file (containing structs) to linked list c

I am new to c programming. In my program I have to make a database to store students. The program has to allow to enter marks, id, name and store (as struct)in linked list and also store in file and also read from file into linked list. The only problem I have is that when I close the .exe program and then select to read from .txt file into linked list it does not work. No errors came when I compiled the code but when I AFTERWARDS select DISPLAY REPORT OF ALL STUDENTS no records are shown. This is a minor problem, please suggest how to fix this.
My code is as follows:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct phbook
{
int number;
char name[20];
int mark;
struct part *next;
};
//struct phbook *find_student(int number);
//void insertFull(struct phbook* list, int number, char Name[10],int mark);
struct phbook* insert(struct phbook *list);
struct phbook* insertFull(struct phbook* list,
int number,
char Name[10],
int mark);
void readFile(struct phbook* list);
int main(void)
{
struct phbook *list = NULL;
int code;
int opt1;
int courses, i, k, j, counter;
for (;;)
{
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code)
{
case 1:
list = insert(list);
break;
case 2: //search();
break;
break;
case 3:
print(list);
break;
case 4:
saveToFile(list);
break;
case 5:
readFile(list);
break;
default:
printf("Illegal code\n");
}
printf("\n");
}
}
/*struct phbook *find_student(int number)
{
struct phbook *p;
for (p = list; p != NULL && number != p->number; p = p->next);//was sorted
if (p != NULL && number == p->number)
return p;
return NULL;
}*/
struct phbook* insert(struct phbook *list)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL)
{
printf("db full er1.\n");
return NULL;
}
printf("enter student id");
scanf("%d", &new_node->number);
for (cur = list, prev = NULL; cur != NULL && new_node->number > cur->number;
prev = cur, cur = cur->next)
;
printf("Enter name: ");
scanf("%s", &new_node->name); //readline(new_node->name, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->mark);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
return list;
}
void search(void)
{
int number;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &number);
p = find_student(number);
if (p != NULL)
{
printf("Name: %s\n", p->name);
printf("Marks: %d\n", p->mark);
}
else
printf("student not found.\n");
}
void print(struct phbook *list)
{
struct phbook *p;
printf("Student_Number Student_Name Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->number, p->name, p->mark);
}
void saveToFile(struct phbook *list)
{
FILE* fp;
fp = fopen("results.txt", "w");
struct phbook* cur = list; //he
while (cur != NULL)
{
fprintf(fp, "%s\n", list->name);
fprintf(fp, "%d\n", cur->number);
fprintf(fp, "%d\n", cur->mark);
cur = cur->next;
}
fclose(fp);
}
void readFile(struct phbook* list)
{
FILE* fp;
if (!(fp = fopen("results.txt", "r")))
printf("File NOT Found");
else
{
struct phbook *cur;
struct phbook *prev;
char TempName[10];
int TempNumber;
int TempMark;
int done = 0;
int count = 0;
int success; //dummy
cur = list; //sets it to the head (first nde of ll)
if (list == NULL)
;
printf("List is null\n");
if (cur == NULL)
printf("List is null\n");
while (cur != NULL)
{
cur = cur->next;
}
while (done == 0)
{
success = fscanf(fp, "%s", TempName);
if (success == 1)
{
success = fscanf(fp, "%d", &TempNumber);
if (success == 1)
{
success = fscanf(fp, "%d", &TempMark);
if (success == 1)
{
insertFull(list, TempNumber, TempName, TempMark);
}
}
}
else
{
done = 1;
}
}
}
}
struct phbook* insertFull(struct phbook* list,
int number,
char Name[10],
int mark)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL)
{
printf("db full er1.\n");
return;
}
for (cur = list, prev = NULL; cur != NULL && new_node->number > cur->number;
prev = cur, cur = cur->next)
;
new_node->number = number;
new_node->mark = mark;
strcpy(new_node->name, Name);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
return list;
}
the screenshot is this:
Screen shot was already MIA when I got here - user4581301
If you execute your application the main list will be null.
When you then call readFile(), you will not update the list in main. You just pass the pointer by value, so that you'll update the local paramater of that function. By the way, the call to insertFull() has the almost the same problem: it returns the list following the insertion, but you ignore this return.
Change these two functions, so that they both return the list, like you do for insert():
struct phbook* readFile(struct phbook* list)
{
...
list = insertFull(list, TempNumber, TempName, TempMark);
...
return list;
}
and of course adapt your function prototype at the beginning, and updated the menu handling:
case 5:
list = readFile(list);
break;
the following code compiles cleanly
However, it will not link because the function: find_student() is commented out.
Examination of the code indicates it will not fully perform all the desired functionality. So you will still have to debug the execution.
Suggest using a debugger, like gdb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME_LEN (20)
struct phbook
{
int studentID;
char studentName[ MAX_NAME_LEN ];
int studentGrade;
struct phbook *next;
};
// prototypes
struct phbook* insert(struct phbook* );
struct phbook* find_student( int );
void insertFull( struct phbook*, int, char *, int );
void readFile ( struct phbook* );
void print ( struct phbook* );
void saveToFile( struct phbook* );
int main(void)
{
struct phbook *list = NULL;
int code;
for (;;)
{
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code)
{
case 1:
list = insert(list);
break;
case 2: //search();
break;
break;
case 3:
print(list);
break;
case 4:
saveToFile(list);
break;
case 5:
readFile(list);
break;
default:
printf("Illegal code\n");
} // end switch
printf("\n");
} // end for()
} // end function: main
#if 0
struct phbook *find_student(int studentID)
{
struct phbook *p;
for (p = list; p != NULL && studentID != p->studentID; p = p->next);//was sorted
return p;
} // end function: find_student
#endif
struct phbook* insert(struct phbook *list)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
if( NULL == (new_node = malloc(sizeof(struct phbook)) ) )
{
perror( "malloc for struct phbook failed");
return NULL;
}
// implied else malloc successful
printf("enter student id");
scanf("%d", &new_node->studentID);
for (cur = list, prev = NULL;
cur != NULL && new_node->studentID > cur->studentID;
prev = cur, cur = cur->next)
{
;
}
printf("Enter studentName: ");
scanf("%19s", new_node->studentName); //readline(new_node->studentName, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->studentGrade);
new_node->next = cur;
if (!prev)
{
list = new_node;
}
else
{
prev->next = new_node;
}
return list;
} // end function: search
void search(void)
{
int studentID;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &studentID);
if( NULL == (p = find_student(studentID) ) )
{
printf("studentName: %s\n", p->studentName);
printf("Marks: %d\n", p->studentGrade);
}
else
{
printf("student not found.\n");
}
} // end function: search
void print(struct phbook *list)
{
struct phbook *p;
printf("Student_Number Student_studentName Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->studentID, p->studentName, p->studentGrade);
} // end function: print
void saveToFile(struct phbook *list)
{
FILE* fp;
fp = fopen("results.txt", "w");
struct phbook* cur = list; //he
while (cur != NULL)
{
fprintf(fp, "%s\n", list->studentName);
fprintf(fp, "%d\n", cur->studentID);
fprintf(fp, "%d\n", cur->studentGrade);
cur = cur->next;
}
fclose(fp);
} // end function: saveToFile
void readFile( struct phbook* list)
{
FILE* fp = NULL;
if (!(fp = fopen("results.txt", "r")))
{
perror( "fopen for read of results.txt failed");
return;
}
//implied else, fopen successful
char TempstudentName[10];
int TempNumber;
int TempMark;
while (3 == fscanf(fp, " %19s %d %d", TempstudentName, &TempNumber, &TempMark ) )
{
insertFull(list, TempNumber, TempstudentName, TempMark);
}
} // end function; readFile
void insertFull(struct phbook* list,
int studentID,
char studentName[10],
int studentGrade)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
if( NULL == (new_node = malloc(sizeof(struct phbook)) ) )
{
perror( "malloc for size of struct phbook failed" );
return;
}
for (cur = list, prev = NULL;
cur != NULL && new_node->studentID > cur->studentID;
prev = cur, cur = cur->next)
{
;
}
new_node->studentID = studentID;
new_node->studentGrade = studentGrade;
strcpy(new_node->studentName, studentName);
new_node->next = cur;
if (!prev)
{
list = new_node;
}
else
{
prev->next = new_node;
}
} // end function: insertFull
I did not add all the necessary error checking, so you will have to do that.
I added the missing prototypes
I corrected several minor coding errors

Printing strings in linked lists

So I'm having trouble getting my program to print both the strings I input, or however many you want to put in the list, it always prints out the last string inputted multiple times. I am sorry about all the commented out code, most of it you don't need to read.
#include<stdio.h>
#include<stdlib.h>
struct node{
char *data;
struct node *next;
}*head;
typedef struct node NODE;
// Function prototypes
void append(char myStr[]);
void add( char myStr[] );
//void addafter(char myStr[], int loc);
void insert(char myStr[]);
int delete(char myStr[]);
void display(struct node *r);
int count();
// main function
int main()
{
int i;
struct node *n;
head = NULL;
char myStr[50];
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d", &i) <= 0)
{
printf("Enter only an Integer\n");
exit(0);
}
else
{
switch(i)
{
case 1:
printf("Enter the name to insert : ");
scanf("%50s", myStr);
insert(myStr);
break;
case 2:
if(head == NULL)
{
printf("List is Empty\n");
}
else
{
printf("Name(s) in the list are : ");
}
display(n);
break;
case 3:
printf("Size of the list is %d\n",count());
break;
case 4:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the myStrber to delete : ");
scanf("%50s",myStr);
if(delete(myStr))
printf("%s deleted successfully\n",myStr);
else
printf("%s not found in the list\n",myStr);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}
// Function definitions
void append(char myStr[])
{
struct node *temp,*right;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = myStr;
right=(struct node *)head;
while(right->next != NULL)
{
right = right->next;
}
right->next = temp;
right = temp;
right->next = NULL;
}
// adding a node to the beginning of the linked list
void add( char myStr[] )
{
struct node *temp;
temp =(struct node *)malloc(sizeof(struct node));
temp->data = myStr;
// only one node on the linked list
if (head == NULL)
{
head = temp;
head->next = NULL;
}
else
{
temp->next = head;
head = temp;
}
}
void insert(char myStr[])
{
int c = 0;
struct node *temp;
temp = head;
if(temp == NULL)
{
add(myStr);
}
else
{
append(myStr);
}
}
int delete(char myStr[])
{
struct node *temp, *prev;
temp = head;
while(temp != NULL)
{
if(temp->data == myStr)
{
if(temp == head)
{
head = temp->next;
head = (*temp).next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r = head;
if(r == NULL)
{
return;
}
while(r != NULL)
{
printf("%s ", r->data);
r = r->next;
if(r == NULL)
{
printf("\nOur linked list is finished!");
}
}
printf("\n");
}
int count()
{
struct node *n;
int c = 0;
n = head;
while(n != NULL)
{
n = n->next;
c++;
}
return c;
}
The problem seems to be that myStr at main function is a char[], so it's content is overritten every time you insert data. Notice that struct node data field is a char*, it's just pointing to myStr address.
Hope this help!
Your program has only one place to write your input, myStr.
With each input, myStr is erased and a something else is written to myStr.
The data member of all of the nodes, points to myStr. myStr will only contain the last input.
The display() function asks each node what is data. data points to myStr so each node prints the contents of myStr. myStr will only contain the last input so all the nodes print the last input.
To fix this, in the add() and append() functions, you need to give the data member some memory by using malloc(). Then copy the contents of myStr to the data member by using strcpy().
temp->data = malloc ( strlen ( myStr) + 1);
strcpy ( temp->data, myStr);
Do this instead of temp->data = myStr;
You will need #include<string.h>
The memory will need to be free()'d in the delete() function.
free(temp->data);
Do this before freeing temp
char *data
that variable from struct is always assigned with the address of myStr as its a pointer it would only show you the value of myStr

Resources