Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Writing a program in C please help!! My file worked fine before but when I put t

ID: 3708383 • Letter: W

Question

Writing a program in C please help!!

My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below.

example input form commnd line file:

The directed edges in this example are: A can go to both B and C. B can go to C. E can go to X. And C to D

The output i am looking for is:

Vertex:              Adjacent to:

A                      B,C

B                      C

C                      D

D

E                      X

X

c code:

List.h

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

//structure list
struct list
{

char vertex, adjacent;

struct list * next;

};

typedef struct list List;


#endif // LIST_H_INCLUDED

link.c

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

//create link function
void createlink(char ch1, char ch2, List *head)

{

List * th;

if (head == NULL)

{

head = (List * ) malloc(sizeof(List));

head-> vertex = ch1;

head -> adjacent = ch2;

head -> next = NULL;

return;

}

th = head;

while (th -> next != NULL)

th = th -> next;

th -> next = (List * ) malloc(sizeof(List));

th -> next -> vertex = ch1;

th -> next -> adjacent = ch2;

th -> next -> next = NULL;

return;

}

display.c

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

//display function
void displayadj(char ch, List *head)

{

List * th = head;

while (th != NULL)

{

if (th -> vertex == ch)

printf("%c ", th -> adjacent); //if a vertex match print its adjacent

th = th -> next;

}

}

main.c

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

//function declarations
void createlink(char ch1, char ch2, List *head);
void displayadj(char ch, List *head);

//main functions
int main(int argc, char** argv)
{
List * head = NULL;
FILE * f1;

if(argc == 2){
f1 = fopen(argv[1], "r");

char ch, ch1;

int array[26] = {
0
};

if (f1 == NULL)

{

printf("error no such file ");

return 0;

}

while ((ch = fgetc(f1)) != EOF)

{

if (ch >= 'A' && ch <= 'Z')

if (array[ch - 'A'] == 0)

{

array[ch - 'A'] = 1;

}

}

f1 = fopen(argv[1], "r");

int num = 0;

char ch2;

//printf("i'm displaying the file now ");

while ((ch = fgetc(f1)) != EOF)

{

if (ch >= 'A' && ch <= 'Z')

{

num++;

if (num % 2 == 0) //if a char between A and Z comes again in a line it takes as ch1 adjacent

{

ch2 = ch;

createlink(ch1, ch2 , head); //create link as vetex ch1,adjacent ch2

num = 0;

//printf("%c %c ",ch1,ch2);

} else

{

ch1 = ch;

}

}

}

int i = 0;

printf("vetex: adjacent vertices: ");

for (; i < 26; i++)

{

if (array[i] == 1)

{
ch = 'A' + i;

printf("%c ", ch);

displayadj(ch,head);

printf(" ");

}

}
}

else{
//printf("Please provide one command line argument");
}
}

Explanation / Answer

#ifndef LIST_H_INCLUDED

#define LIST_H_INCLUDED

//structure list

struct list

{

char vertex, adjacent;

struct list * next;

};

typedef struct list List;

#endif // LIST_H_INCLUDED

link.c

#include <stdio.h>

#include <stdlib.h>

#include "List.h"

//create link function

void createlink(char ch1, char ch2, List *head)

{

List * th;

if (head == NULL)

{

head = (List * ) malloc(sizeof(List));

head-> vertex = ch1;

head -> adjacent = ch2;

head -> next = NULL;

return;

}

th = head;

while (th -> next != NULL)

th = th -> next;

th -> next = (List * ) malloc(sizeof(List));

th -> next -> vertex = ch1;

th -> next -> adjacent = ch2;

th -> next -> next = NULL;

return;

}

display.c

#include <stdio.h>

#include <stdlib.h>

#include "List.h"

//display function

void displayadj(char ch, List *head)

{

List * th = head;

while (th != NULL)

{

if (th -> vertex == ch)

printf("%c ", th -> adjacent); //if a vertex match print its adjacent

th = th -> next;

}

}

main.c

#include <stdio.h>

#include <stdlib.h>

#include "List.h"

//function declarations

void createlink(char ch1, char ch2, List *head);

void displayadj(char ch, List *head);

//main functions

int main(int argc, char** argv)

{

List * head = NULL;

FILE * f1;

if(argc == 2){

f1 = fopen(argv[1], "r");

char ch, ch1;

int array[26] = {

0

};

if (f1 == NULL)

{

printf("error no such file ");

return 0;

}

while ((ch = fgetc(f1)) != EOF)

{

if (ch >= 'A' && ch <= 'Z')

if (array[ch - 'A'] == 0)

{

array[ch - 'A'] = 1;

}

}

f1 = fopen(argv[1], "r");

int num = 0;

char ch2;

//printf("i'm displaying the file now ");

while ((ch = fgetc(f1)) != EOF)

{

if (ch >= 'A' && ch <= 'Z')

{

num++;

if (num % 2 == 0) //if a char between A and Z comes again in a line it takes as ch1 adjacent

{

ch2 = ch;

createlink(ch1, ch2 , head); //create link as vetex ch1,adjacent ch2

num = 0;

//printf("%c %c ",ch1,ch2);

} else

{

ch1 = ch;

}

}

}

int i = 0;

printf("vetex: adjacent vertices: ");

for (; i < 26; i++)

{

if (array[i] == 1)

{

ch = 'A' + i;

printf("%c ", ch);

displayadj(ch,head);

printf(" ");

}

}

}

else{

//printf("Please provide one command line argument");

}

}