Can someone provide codes in C for the following questions:
Write C programs grah.h and graph.c to implement adjacent list graph representation and operation functions. Test with q1.c.
graph.h
#ifndef GRAPH_H #define GRAPH_H #include <stdio.h> #include <stdlib.h> #define INFINITY 99999 typedef struct adjnode { int vertex; int weight; struct adjnode *next; } ADJNODE; typedef struct graph { int order; //number of nodes int size; //number of edges ADJNODE **adjlist; //pointer to an array of pointers of neighbors } GRAPH; /* create and return a new adjacent list graph of order n */ GRAPH *new_graph(int n); /* add a new edge (from, to, weight) to the graph passed by GRAPH *g, if the edge (from, to) exists, update its weight*/ void add_edge(GRAPH *g, int from, int to, int weight); /* return the weight of edge (from, to) if exists, otherwise return INFINITY*/ int get_weight(GRAPH *g, int from, int to); /* display the graph the proper format*/ void display_graph(GRAPH *g); /* clean the graph by free all dynamically allocated memory*/ void clean_graph(GRAPH **gp);
graph.c
GRAPH *new_graph(int vertex_number) { // your implementation } void add_edge(GRAPH *g, int from, int to, int weight) { // your implementation } int get_weight(GRAPH *g, int from, int to) { // your implementation } void clean_graph(GRAPH **gp) { // your implementation } void display_graph(GRAPH *g) { if (g == NULL) return; printf("\nweighted graph in adjacency list"); printf("\norder: %d", g->order); printf("\nsize: %d", g->size); printf("\nnode from: (to weight)"); int i; ADJNODE *ptr; for (i = 0; i < g->order; i++) { printf("\nnode %d:", i); ptr = g->adjlist[i]; while (ptr != NULL) { printf(" (%d, %d)", ptr->vertex, ptr->weight); ptr = ptr->next; } } }
q1.c
#include "graph.h" int main(){ GRAPH *graph = new_graph(5); add_edge(graph, 0, 1, 7); add_edge(graph, 1, 0, 7); add_edge(graph, 0, 2, 3); add_edge(graph, 2, 0, 3); add_edge(graph, 1, 2, 4); add_edge(graph, 2, 1, 4); add_edge(graph, 2, 3, 10); add_edge(graph, 3, 2, 10); add_edge(graph, 1, 3, 9); add_edge(graph, 3, 1, 9); add_edge(graph, 1, 4, 11); add_edge(graph, 4, 1, 11); display_graph(graph); clean_graph(&graph); printf("\ngraph is deleted"); return 0; } /* gcc graph.h graph.c q1.c -o q1 q1 Weighted graph in adjacency list node from: (to weight) ... node 0: (1, 7) (2, 3) node 1: (0, 7) (2, 4) (3, 9) (4, 11) node 2: (0, 3) (1, 4) (3, 10) node 3: (2, 10) (1, 9) node 4: (1, 11) Graph is deleted */
Write C programs edgelist.h and edgelist.c to implement edgelist graph data structure and operation functions. Test with q2.c.
edgelist.h
typedef struct edge { int from; int to; int weight; struct edge *next; } EDGE; typedef struct edgelist { int size; //number of edges EDGE *start; //pointer to the start of edge list EDGE *end; //pointer to end of edge list } EDGELIST; /* create and return a new edge list graph*/ EDGELIST *new_edgelist(); /* add an new edge at the end of the linked list of edges*/ void add_edge_end(EDGELIST *g, int from, int to, int weight); /* add an new edge at the start of the linked list of edges*/ void add_edge_start(EDGELIST *g, int from, int to, int weight); /* get weight of the edge list graph*/ int weight_edgelist(EDGELIST *g); /* display edge list graph*/ void display_edgelist(EDGELIST *g); /* clean the graph by free all dynamically allocated memory*/ void clean_edgelist(EDGELIST **gp);
edgelist.c
EDGELIST *new_edgelist() { // your implementation } void add_edge_end(EDGELIST *g, int from, int to, int weight) { // your implementation } void add_edge_start(EDGELIST *g, int from, int to, int weight) { // your implementation } int weight_edgelist(EDGELIST *g) { // your implementation } void clean_edgelist(EDGELIST **gp) { // your implementation } void display_edgelist(EDGELIST *g) { if (g == NULL) return; printf("\nweighted graph in edge list"); printf("\nsize: %d", g->size); printf("\nformat: (from, to, weight)"); EDGE *p = g->start; while (p) { printf("\n(%d, %d, %d)", p->from, p->to, p->weight); p = p->next; } }
q2.c
#include "edgelist.h" int main(){ EDGELIST *elg = new_edgelist(); add_edge_end(elg, 0, 2, 3); add_edge_end(elg, 2, 1, 4); add_edge_end(elg, 1, 3, 9); add_edge_end(elg, 1, 4, 11); display_edgelist(elg); printf("\nweight: %d", weight_edgelist(elg)); clean_edgelist(&elg); printf("\ngraph is deleted"); return 0; } /* gcc edgelist.h edgelist.c q2.c -o q2 q2 weighted graph in edge list size: 4 format: (from, to, weight) (0, 2, 3) (2, 1, 4) (1, 3, 9) (1, 4, 11) weight: 27 graph is deleted */
graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <stdio.h>
#include <stdlib.h>
#define INFINITY 99999
typedef struct adjnode {
int vertex;
int weight;
struct adjnode *next;
} ADJNODE;
typedef struct graph {
int order; //number of nodes
int size; //number of edges
ADJNODE **adjlist; //pointer to an array of pointers of neighbors
} GRAPH;
/* create and return a new adjacent list graph of order n */
GRAPH *new_graph(int n);
/* add a new edge (from, to, weight) to the
graph passed by GRAPH *g, if the edge (from, to) exists, update its weight*/
void add_edge(GRAPH *g, int from, int to, int weight);
/* return the weight of edge (from, to) if exists, otherwise return INFINITY*/
int get_weight(GRAPH *g, int from, int to);
/* display the graph the proper format*/
void display_graph(GRAPH *g);
/* clean the graph by free all dynamically allocated memory*/
void clean_graph(GRAPH **gp);
#endif
graph.c
#include"graph.h"
GRAPH *new_graph(int vertex_number)
{
GRAPH* g = (GRAPH*)malloc(sizeof(GRAPH));
g->order = vertex_number;
g->size = 0;
g->adjlist = (ADJNODE**)malloc(sizeof(ADJNODE) * vertex_number);
return g;
}
void add_edge(GRAPH *g, int from, int to, int weight)
{
ADJNODE* adjHead = NULL;
ADJNODE* newAdjNode = NULL;
if(g == NULL)
return;
if(g->adjlist == NULL)
return;
adjHead = g->adjlist[from];
newAdjNode = (ADJNODE*)malloc(sizeof(ADJNODE));
newAdjNode->vertex = to;
newAdjNode->weight = weight;
newAdjNode->next = NULL;
g->size++;
//if the edge (from, to) exists, update its weight
//and return, no need to iterate next
if(adjHead != NULL){
if(adjHead->vertex == to){
adjHead->weight = weight;
free(newAdjNode);
newAdjNode = NULL;
return;
}
}
if(adjHead == NULL){
g->adjlist[from] = newAdjNode;
}else{
while(adjHead->next != NULL){
//if the edge (from, to) exists, update its weight
if(adjHead->vertex == to){
adjHead->weight = weight;
free(newAdjNode);
newAdjNode = NULL;
return;
}
adjHead = adjHead->next;
}
adjHead->next = newAdjNode;
}
}
int get_weight(GRAPH *g, int from, int to)
{
ADJNODE* adjHead = NULL;
if(g == NULL)
return INFINITY;
if(g->adjlist == NULL)
return INFINITY;
}
void clean_graph(GRAPH **gp)
{
ADJNODE* adjHead = NULL;
ADJNODE* temp = NULL;
GRAPH* g = *gp;
int i;
for(i = 0; i < g->order; i++){
adjHead = g->adjlist[i];
while(adjHead != NULL){
temp = adjHead->next;
free(adjHead);
adjHead = NULL;
adjHead = temp;
}
}
free(g->adjlist);
g->adjlist = NULL;
free(*gp);
*gp = NULL;
}
void display_graph(GRAPH *g) {
if (g == NULL) return;
printf("\nweighted graph in adjacency list");
printf("\norder: %d", g->order);
printf("\nsize: %d", g->size);
printf("\nnode from: (to weight)");
int i;
ADJNODE *ptr;
for (i = 0; i < g->order; i++) {
printf("\nnode %d:", i);
ptr = g->adjlist[i];
while (ptr != NULL) {
printf(" (%d, %d)", ptr->vertex, ptr->weight);
ptr = ptr->next;
}
}
printf("\n");
}
q1.c
#include "graph.h"
int main(){
GRAPH *graph = new_graph(5);
add_edge(graph, 0, 1, 7);
add_edge(graph, 0, 1, 123);// for updatng weight
add_edge(graph, 1, 0, 7);
add_edge(graph, 0, 2, 3);
add_edge(graph, 2, 0, 3);
add_edge(graph, 1, 2, 4);
add_edge(graph, 2, 1, 4);
add_edge(graph, 2, 3, 10);
add_edge(graph, 3, 2, 10);
add_edge(graph, 1, 3, 9);
add_edge(graph, 3, 1, 9);
add_edge(graph, 1, 4, 11);
add_edge(graph, 4, 1, 11);
display_graph(graph);
clean_graph(&graph);
printf("graph is deleted\n");
return 0;
}
edgelist.h
#ifndef GRAPH_H
#define GRAPH_H
#include <stdio.h>
#include <stdlib.h>
typedef struct edge {
int from;
int to;
int weight;
struct edge *next;
} EDGE;
typedef struct edgelist {
int size; //number of edges
EDGE *start; //pointer to the start of edge list
EDGE *end; //pointer to end of edge list
} EDGELIST;
/* create and return a new edge list graph*/
EDGELIST *new_edgelist();
/* add an new edge at the end of the linked list of edges*/
void add_edge_end(EDGELIST *g, int from, int to, int weight);
/* add an new edge at the start of the linked list of edges*/
void add_edge_start(EDGELIST *g, int from, int to, int weight);
/* get weight of the edge list graph*/
int weight_edgelist(EDGELIST *g);
/* display edge list graph*/
void display_edgelist(EDGELIST *g);
/* clean the graph by free all dynamically allocated memory*/
void clean_edgelist(EDGELIST **gp);
#endif
edgelist.c
#include"edgelist.h"
EDGELIST *new_edgelist()
{
EDGELIST* edgelist = (EDGELIST*)malloc(sizeof(EDGELIST));
edgelist->size = 0;
edgelist->start = NULL;
edgelist->end = NULL;
}
void add_edge_end(EDGELIST *g, int from, int to, int weight)
{
EDGE* newEdge = NULL;
if(g == NULL)
return;
newEdge = (EDGE*)malloc(sizeof(EDGE));
newEdge->from = from;
newEdge->to = to;
newEdge->weight = weight;
newEdge->next = NULL;
if(g->end == NULL){
g->end = newEdge;
g->size++;
if(g->start == NULL)
g->start = newEdge;
}else{
g->end->next = newEdge;
g->end = newEdge;
g->size++;
}
}
void add_edge_start(EDGELIST *g, int from, int to, int weight)
{
EDGE* newEdge = NULL;
if(g == NULL)
return;
newEdge = (EDGE*)malloc(sizeof(EDGE));
newEdge->from = from;
newEdge->to = to;
newEdge->weight = weight;
newEdge->next = NULL;
if(g->start == NULL){
g->start = newEdge;
g->size++;
if(g->end == NULL)
g->end = newEdge;
}else{
newEdge->next = g->start;
g->start = newEdge;
g->size++;
}
}
int weight_edgelist(EDGELIST *g)
{
EDGE *edgeHead = NULL;
int weight = 0;
if(g == NULL)
return weight;
edgeHead = g->start;
while(edgeHead != NULL){
weight += edgeHead->weight;
edgeHead = edgeHead->next;
}
return weight;
}
void clean_edgelist(EDGELIST **gp)
{
EDGE *edgeHead = NULL;
EDGE *temp = NULL;
if(*gp == NULL)
return;
edgeHead = (*gp)->start;
while(edgeHead != NULL){
temp = edgeHead->next;
free(edgeHead);
edgeHead = NULL;
edgeHead = temp;
}
free(*gp);
*gp = NULL;
}
void display_edgelist(EDGELIST *g) {
if (g == NULL) return;
printf("weighted graph in edge list\n");
printf("size: %d\n", g->size);
printf("format: (from, to, weight)\n");
EDGE *p = g->start;
while (p) {
printf("(%d, %d, %d)\n", p->from, p->to, p->weight);
p = p->next;
}
}
q2.c
#include "edgelist.h"
int main()
{
EDGELIST *elg = new_edgelist();
add_edge_end(elg, 0, 2, 3);
add_edge_end(elg, 2, 1, 4);
add_edge_end(elg, 1, 3, 9);
add_edge_end(elg, 1, 4, 11);
add_edge_start(elg, 0, 4, 1);
display_edgelist(elg);
printf("weight: %d\n", weight_edgelist(elg));
clean_edgelist(&elg);
printf("graph is deleted\n");
return 0;
}
Can someone provide codes in C for the following questions: Write C programs grah.h and graph.c...
I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX]; struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL) { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...
I need help on this Systems review please! it's due by midnight monday. Question 1 Not yet answered Points out of 1.00 Flag question Question text Using these declarations: int * numberPointers[3]; int * pointer; int number; Which of the following statements would generate a warning or error? Select one: a. number = pointer; b. *pointer = number; c. pointer = numberPointers; d. numberPointers[2] = &number; e. a., b., and d. f. a. and c. Question 2 Not yet answered...
How would I traverse through this graph? Provide example code, please! class Edge { int src, dest; Edge(int src, int dest) { this.src = src; this.dest = dest; } }; // class to represent a graph object class Graph { // A list of lists to represent adjacency list List<List<Integer>> adj = new ArrayList<>(); // Constructor to construct graph public Graph(List<Edge> edges) { // allocate memory for adjacency list for (int i = 0; i < edges.size(); i++) { adj.add(i,...
In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int source, int dest) returns 0 if I can reach the destination from source, -1 otherwise ( using BFS) has_cycle(graph_t * g) returns 0 if there is a cycle in the graph, -1 otherwise (using BFS or DFS) print_path(graph_t * g, int source, int dest) prints any path from source to destination if there exists one (Choose either BFS or DFS, typically DFS is much...
Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...
Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() function. Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...
//Graph Class: import java.util.ArrayList; //Graph is a class whose objects represent graphs. public class Graph { ArrayList<Node> nodeList; ArrayList<Edge> edgeList; public Graph() { nodeList = new ArrayList<Node>(); edgeList = new ArrayList<Edge>(); } public ArrayList<Node> getNodeList() { return nodeList; } public ArrayList<Edge> getEdgeList() { return edgeList; } public void addNode(Node n) { nodeList.add(n); } public void addEdge(Edge e) { edgeList.add(e); } public String toString() { String s = "Graph g.\n"; if (nodeList.size() > 0) { for (Node n : nodeList) { // Print node info String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n"; s = s.concat(t); } s = s.concat("\n"); } return s; } } // Node Class: import java.util.ArrayList; // Node is a class whose objects represent nodes (a.k.a., vertices) in the graph. public class Node { String name; String val; // The value of the Node String abbrev; // The abbreviation for the Node ArrayList<Edge> outgoingEdges; ArrayList<Edge> incomingEdges; String color; //Create the color of the TYPE Node List int start; //Create the Starting Time int end; //Create the Ending Time public Node( String theAbbrev ) { setAbbrev( theAbbrev ); val = null; name = null; outgoingEdges = new ArrayList<Edge>(); incomingEdges = new ArrayList<Edge>(); } public String getAbbrev() { return abbrev; } public String getName() { return name; } public String getVal() { return val; } public ArrayList<Edge> getOutgoingEdges() { return outgoingEdges; } public ArrayList<Edge> getIncomingEdges() { return incomingEdges;...
ProgramIntro.pngProgramIntro2.pngProgramIntro1.pngProgram1.pngProgram2.pngGraph Class:import java.util.ArrayList;//Graph is a class whose objects represent graphs. public class Graph { ArrayList<Node> nodeList; ArrayList<Edge> edgeList; public Graph() { nodeList = new ArrayList<Node>(); edgeList = new ArrayList<Edge>(); } public ArrayList<Node> getNodeList() { return nodeList; } public ArrayList<Edge> getEdgeList() { return edgeList; } public void addNode(Node n) { nodeList.add(n); } public void addEdge(Edge e) { edgeList.add(e); } public String...
Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...
CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...