How would I implement a stack for an array of structs? - c

I have a small application that I am creating using GCC in Ubuntu 10.04. I have a header file and a source file.
My header file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DECKSZ 52
#define HAND_SIZE 5
#define STACKMAX 52
#define EMPTY -1
#define FULL (STACKMAX-1)
typedef enum boolean {false, true} boolean;
typedef struct card {
enum pip {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
enum suit {SPADES, CLUBS, HEARTS, DIAMONDS} suits;
char cardName[20];
} card;
typedef struct stack {
card s[STACKMAX];
int top;
} stack;
extern card deck[];
void initDeck(card[]);
void labelCards(card[]);
void shuffleDeck(card[]);
boolean dealHand(card[], stack*);
void displayHand(card*);
void arrangeHand(card*);
void swap(card*, card*);
boolean isFlush(card[]);
boolean isStraight(card[]);
boolean isXOfAKind(card[], int, enum pip);
boolean isStraightFlush(card[]);
boolean isFullHouse(card[]);
boolean isTwoPair(card[]);
boolean isEmpty(stack*);
boolean isFull(stack*);
void push(card*, stack*);
card pop(stack*);
void reset(stack*);
My source file:
#include "Poker.h"
int main(void) {
int i;
int flushCount = 0;
int straightCount = 0;
int xOfAKindCount = 0;
int straightFlushCount = 0;
int fullHouseCount = 0;
int isTwoPairCount = 0;
stack *stkDeck = stack;
stack *stkHand = stack;
card deck[DECKSZ] = {0};
initDeck(deck);
labelCards(deck);
for (i = 0; i < DECKSZ; i++) {
push(&deck[i], stkDeck);
}
/*do {*/
flushCount = 0;
straightCount = 0;
xOfAKindCount = 0;
straightFlushCount = 0;
fullHouseCount = 0;
isTwoPairCount = 0;
shuffleDeck(deck);
displayHand(deck);
arrangeHand(&deck[0]);
flushCount = isFlush(&deck[0]);
straightCount = isStraight(&deck[0]);
xOfAKindCount = isXOfAKind(&deck[0], 2, 0);
straightFlushCount = isStraightFlush(&deck[0]);
fullHouseCount = isFullHouse(&deck[0]);
isTwoPairCount = isTwoPair(&deck[0]);
printf("Flush Count: %d\n", flushCount);
printf("Straight Count: %d\n", straightCount);
printf("X Of A Kind Count: %d\n", xOfAKindCount);
printf("Straight Flush Count: %d\n", straightFlushCount);
printf("Full House Count: %d\n", fullHouseCount);
printf("Two Pair Count: %d\n", isTwoPairCount);
/*} while (1);*/
return EXIT_SUCCESS;
}
void initDeck(card deck[]) {
int counter;
for (counter = 0; counter < DECKSZ; counter++) {
deck[counter].pips = (const)((counter % 13) + 1);
deck[counter].suits = (const)(counter / 13);
}
}
void labelCards(card deck[]) {
const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
const char *suitNames[] = {" of Spades"," of Hearts"," of Diamonds"," of Clubs"};
int i, tmpPip = 0, tmpSuit = 0;
for (i = 0; i < DECKSZ; i++) {
tmpPip = (deck[i].pips) - 1;
tmpSuit = (deck[i].suits);
strcpy(deck[i].cardName, pipNames[tmpPip]);
strcat(deck[i].cardName, suitNames[tmpSuit]);
}
}
void shuffleDeck(card deck[]) {
int i, j;
for (i = 0; i < DECKSZ; i++) {
j = rand() % DECKSZ;
swap(&deck[i], &deck[j]);
}
}
boolean dealHand(card deck[], stack *stkHand) {
boolean successfulDeal = ((boolean) (0));
int i;
for (i = 0; i < HAND_SIZE; i++) {
push(&deck[i], stkHand);
}
return successfulDeal;
}
void displayHand(card hand[]) {
int i;
for (i = 0; i < HAND_SIZE; i++) {
printf("%s\n", hand[i].cardName);
}
}
void arrangeHand(card *hand) {
int i, j;
for (i = HAND_SIZE-1; i >= 0; i--) {
for (j = 0; j < i; j++) {
if ((hand+j)->pips > (hand+j+1)->pips)
swap(hand+j, hand+j+1);
}
}
}
void swap(card *c1, card *c2) {
card temp;
temp = *c1;
*c1 = *c2;
*c2 = temp;
}
boolean isFlush(card hand[]) {
int i, count = 0, result = 0;
for (i = 0; i < HAND_SIZE-1; i++) {
if (hand[i].suits != hand[i+1].suits) {
count++;
}
}
if (count == HAND_SIZE)
result = 1;
return ((boolean) (result));
}
boolean isStraight(card hand[]) {
int i, count = 0, result = 0;
for (i = 0; i < HAND_SIZE - 1; i++) {
if (hand[i].pips == (hand[i+1].pips + 1)) {
count++;
}
}
if (count == HAND_SIZE)
result = 1;
return ((boolean) (result));
}
boolean isXOfAKind(card hand[], int x, enum pip pipsIgnored) {
int i, count = 0, result = 0;
for (i = 0; i < HAND_SIZE - 1; i++) {
if (hand[i].pips == hand[i+1].pips) {
if (hand[i].pips != pipsIgnored) {
count++;
}
}
}
if (count == (x - 1))
result = 1;
return count;
}
boolean isStraightFlush(card hand[]) {
int result = 0;
result = isFlush(hand);
result = isStraight(hand);
return ((boolean) (result));
}
boolean isFullHouse(card hand[]) {
int result = 0;
result = isXOfAKind(hand, 3, 0);
result = isXOfAKind(hand, 2, 0);
return ((boolean) (result));
}
boolean isTwoPair(card hand[]) {
int result = 0;
result = isXOfAKind(hand, 2, hand->pips);
result = isXOfAKind(hand, 2, hand->pips);
return ((boolean) (result));
}
boolean isEmpty(stack *stk) {
return ((boolean) (stk->top = EMPTY));
}
boolean isFull(stack *stk) {
return ((boolean) (stk->top == FULL));
}
void push(card *c, stack *stk) {
stk->top++;
stk->s[stk -> top] = *c;
}
card pop(stack *stk) {
return (stk->s[stk->top--]);
}
void reset(stack *stk) {
stk->top = EMPTY;
}
My question pertains to the deck[] array inside main(). I would like to implement it as a stack so that each hand comes off the stack as 5 card structs when a hand is "drawn". Once the deck is out of cards, I would like it to create a newly shuffled deck of cards and continue to deal hands (meaning the stack would be repopulated to 52 cards and continue to allow hands to be popped from the stack).
Would anybody be able to help me implement a stack for my array of card structs (my "deck")? Thank you!

You are asking for an implementation of a stack using an array. I found an implementation using Google Search (not saying it is any good). Here are PUSH and POP parts. The stuff in "main" is just pro forma stuff. You should be able to manipulate this code to do what you want.
/*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/
#include <stdio.h>
#define MAX 52
int top, status;
/*PUSH FUNCTION*/
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
/*POP FUNCTION*/
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
--top;
}
return ret;
}
/*MAIN PROGRAM*/
void main()
{
int stack [MAX], item;
top = -1;
push (stack, item);
item = pop (stack);
}

There are several problems here: How to represent the stack of cards (an easy way to do it is using an array, as the number of elements is bounded and small; have a variable to tell how many cards are left). The other problem is to get the shuffled deck, the simplest solution is the Knuth shuffle. Note that if you take out cards by 5, you will have two cards left over at the end, you'll have to decide what to do with them (just shuffle all 52 cards each time, or shuffle just the 50 that were handed out).

Related

Is there a way to combine two different strings alphabetically using stack in C?

Here is the program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *stack;
int stackindex = 0;
int push(char in){
if (stack==NULL) {
stack = (char *) malloc (sizeof(char));
}
else {
stack = (char *) realloc(stack, sizeof(char)* ++stackindex);
}
stack[stackindex] = in;
return 1;
}
char pop(){
return stack[stackindex--];
}
void show() {
for (int i=stackindex; i>=0;i--) {
printf("%c", pop());
}
}
int main(int argc, char**argv) {
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
int j;
for (int j = 0; j < k ; j++) {
char in = argv [i] [j];
push( in );
}
for (j = i; j < k; j++){
char temp;
if (stack[i] > stack[j]) {
temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
if (i, argc-1) push(' ');
}
show();
return 0;
}
}
I've been trying to make an output from the string "im loving" alphabetically in descending order (ASCII based) using command line arguments like this: vonmliig, but the output was "m i" instead.
Looks like you are trying to sort as you build the stack. I would build the stack first, then sort.
int main(int argc, char**argv)
{
if (argc >= 3) {
// Build stack
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
for (int j = 0; j < k ; j++) {
push(argv[i][j]);
}
}
// Sort
for (int i = 0; i < stackindex; i++) {
for (int j = i; j < stackindex; j++){
if (stack[i] > stack[j]) {
char temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
}
}
show();
}
return 0;
}
Also, there is an error in the handling of stackindex in push().
int push(char in)
{
char *temp = realloc(stack, stackindex + 1);
if (!temp) {
// Handle error
return -1;
}
stack = temp;
stack[stackindex++] = in;
return 1;
}
char pop()
{
if (stackindex) return stack[--stackindex];
else return 0;
}
void show()
{
do {
printf("%c", pop());
} while (stackindex);
}

I am trying to solve a special travelling salesman problem with mpi and c

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <mpi.h>
#define MaxSize 50
typedef int ElementType;
typedef int Position;
typedef int Status;
typedef struct SeqStack
{
ElementType Data[MaxSize];
Position Top;
int (*Push)(struct SeqStack *L);
int (*Pop)(struct SeqStack *L , int e);
int (*isEmpty)(struct SeqStack s);
Status (*isFull)(struct SeqStack s);
}SeqStack;
int Push(SeqStack *L)
{
if(L->Top == 0)
{
return 0;
}
printf("%d ",L->Data[--L->Top]);
return 1;
}
int Pop(SeqStack *L , int e)
{
if(L->Top==MaxSize -1)
{
return 0;
}
L->Data[L->Top++] = e;
return 1;
}
int isEmpty(SeqStack s)
{
if(s.Top != 0)
{
return 1;
}
return 0;
}
Status isFull(SeqStack s)
{
if(s.Top != MaxSize -1)
{
return 1;
}
return 0;
}
int global_max;
int ans = INT_MAX;
void *tsp(int Dis[global_max][global_max],int v[],int N,int count,int currPos,int cost,int sum,int* result,int temp[],int make_log){
temp[0]=1;
//int mode;
//printf("count:%d\n",count);
if(count == N&&Dis[currPos][1]>0){
//printf("Ans:%d,cost:%d\n",ans,cost);
if(make_log == 1){
char str[100]={'\0'};
char output[100]={'\0'};
//printf("Ans:%d,cost:%d\n",ans,cost);
FILE *log;
for(int i=0; i<N;i++){
if (i!=N-1){
sprintf(str,"%d",temp[i]);
strcat(output,str);
strcat(output,",");
}else{
sprintf(str,"%d",temp[i]);
strcat(output,str);
strcat(output,"=");
}
}
sprintf(str,"%d",cost);
strcat(output,str);
strcat(output,"\n");
log=fopen("log.txt","a");
fputs(output,log);
fclose(log);
}
if(sum> cost){
sum = cost;
ans = cost;
//printf("every ans is %d\n\n\n\n",ans);
for (int i=0;i<N;i++){
//printf("temp[%d]=%d,",i,temp[i]);
result[i]=temp[i];
//printf("result[%d]=%d,",i,result[i]);
}
}
//ans = min(ans,cost + Dis[1][currPos]);
return result;
}
for (int i = 1;i<N+1;i++){
//printf("!!!!!!! v[%d] = %d\n",i,v[i]);
if(v[i]==0&&Dis[currPos][i]>0){
//printf("cost + Dis: %d + %d\n",cost,Dis[currPos][i]);
if(cost + Dis[currPos][i] <= ans||count==N-1){
v[i] = 1;
temp[count] = i;
//printf("\ntemp[%d] = %d\n",count,temp[count]);
//printf("currPos:%d,i:%d,count:%d\n",currPos,i,count);
//printf("Ans:%d,cost:%d\n",ans,cost);
result = tsp(Dis,v,N,count + 1,i,cost + Dis[currPos][i],sum,result,temp,make_log);
//mode = 0;
v[i]= 0;
if(count==1){
for(int j = 2;j<N+1;j++){
v[j]= 0;
}
}
}else{
//printf("currPos:%d,i:%d,count:%d\n",currPos,i,count);
temp[count] = i;
for (int k = N-1;k>count;k--){
temp[k] = 0;
}
int v_copy[N];
for (int o = 1;o<N+1;o++){
v_copy[o] = v[o];
// if(currPos==3&&i==4){
// printf("v_copy[%d] = %d//",o,v_copy[o]);
// }
}
for(int j = 2;j< N + 1;j++){
if(v[j]==0){
v[j] = 1;
}
}
result = tsp(Dis,v,N,N,N,cost + Dis[currPos][i],sum,result,temp,make_log);
//printf("Fuck currpos:%d\n",currPos);
for (int o = 1;o<N+1;o++){
v[o] = v_copy[o];
// if(currPos==3&&i==2){
// printf("v[%d] = %d//",o,v[o]);
// }
}
if(count==1){
for(int j = 2;j<N+1;j++){
v[j]= 0;
}
}
}
}
}
return result;
};
void change(int n){
global_max = n;
};
int main(int argc, char const *argv[])
{
int N;
int size = 1;
int count = 1;
int log = 0;
int nthreads,my_rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if(argc > 2){
log = 1;
}
char x[100] = {0};
char filename[100] = {};
strcat(filename,argv[1]);
strcat(filename,".txt");
//printf("%s\n",filename);
FILE *fp=fopen(filename,"r");
if(fp==NULL){
printf("Cannot open the file,strike any key to exit!\n");
getchar();
exit(0);
}
for (int i = 0;!feof(fp);i++){
fscanf(fp,"%hhd",&x[i]);
}
N=x[0];
change(N);
int* result;
result = (int *)malloc(sizeof(int) * global_max);
int Dis[N][N], City[N];
for(int i=1;i<N;i++){
size=size*i;
}
char y[size];
for (int i=1;i<size+1;i++){
y[i] = x[i];
//printf("%d\n",y[i]);
}
for(int i=2; i < N + 1; i++){
for(int j=1; j < i ; j++){
Dis[j][i] = y[count];
Dis[i][j] = y[count];
count+=1;
//printf("(%d,%d),(%d,%d)",j,i,i,j);
//printf("%d\n",Dis[j][i]);
}
}
for(int i=0;i<N;i++){
City[i]=i + 1;//create the number of city with 1 ...... N
}
int curr_constraint = 0;
int v[N+1];
for (int i = 1; i < N+1; i++){
v[i] = 0;
}
for(int i = 1;i<N+1;i++){
curr_constraint += Dis[i][i+1];
}
v[1]= 1;
int sum = INT_MAX;
//printf("orginal ans is %d\n",ans);
//printf("Dis map:\n");
for( int i= 1;i<N+1;i++){
for(int j =1;j<N+1;j++){
if(i==j){
Dis[i][j]=0;
}
//printf("%d ",Dis[i][j]);
}
//printf("\n");
}
//printf("The orginal constraint is %d\n",curr_constraint);
int temp[N];
int* city_divided;
int cityNumber;
int current_ans;
SeqStack s;
if(nthreads > 1){
int remain = N % (nthreads - 1);
//int group_n = N /(nthreads - 1);
if(remain !=0){
if(my_rank <= remain && my_rank != 0){
cityNumber = ((N - remain)/(nthreads - 1)) + 1;
city_divided = (int*)malloc(cityNumber*sizeof(int));
for (int i = 0; i <cityNumber; i++)
{
city_divided[i] = City[(my_rank-1)*cityNumber + 1];
}
}else{
cityNumber = ((N - remain) / (nthreads - 1));
city_divided = (int*)malloc(cityNumber*sizeof(int));
for (int i = 0; i <cityNumber; i++)
{
city_divided[i] = City[(my_rank-1)*cityNumber + 1];
}
}
}else{
cityNumber = N / (nthreads - 1);
city_divided = (int*)malloc(cityNumber*sizeof(int));
for (int i = 0; i <cityNumber; i++)
{
city_divided[i] = City[(my_rank-1)*cityNumber + 1];
}
}
if(my_rank==0){
MPI_Recv(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for(int i=0;i<N;i++){
if(i != N-1){
printf("%d,",*(result+i));
}
else{
printf("%d",*(result+i));
}
}
printf("\n");
printf("Distance: %d\n",ans);
}
if(my_rank!=0){
for (int i = 0 ; i<cityNumber;i++){
current_ans = ans;
while(s.isEmpty(s)){
MPI_Recv(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
result = tsp(Dis,v,N,1,city_divided[i],0,ans,result,temp,log);
s.Pop(&s,ans);
}
result = tsp(Dis,v,N,1,city_divided[i],0,ans,result,temp,log);
if(current_ans - ans > 0){
s.Push(ans);
MPI_Bcast(&ans,1,MPI_INT,0,MPI_COMM_WORLD);
}
}
}
}
else{
result = tsp(Dis,v,N,1,1,0,sum,result,temp,log);
}
for(int i=0;i<N;i++){
if(i != N-1){
printf("%d,",*(result+i));
}
else{
printf("%d",*(result+i));
}
}
printf("\n");
printf("Distance: %d\n",ans);
return 0;
}
I am trying to solve a special travelling salesman problem with mpi and c. I want make the master processor print the final information including the route and the shorest distance. the other processors with all city group run the tsp function, and when it has the shorter distance, use mpi_send or mpi_Bcast to other processors.Follow this logic, I meet a problem when I run the program.
enter image description here

Segfault in Merge - Sort in C

I am trying to sort an array of structures of size 5500 using merge sort.
However, I am getting a segmentation fault pretty quickly because I am not allowed to use VLA. so I have to create 2 extra arrays of size 5500 each time I call merge-sort recursively.
I would appreciate a fix for my problem. I will provide my code here:
void merge(Student rightArr[], Student leftArr[], Student mergedArr[], int sizeOfRight, int sizeOfLeft) {
int rightArrIndex = 0;
int leftArrIndex = 0;
int mergedArrIndex = 0;
while (leftArrIndex < sizeOfLeft && rightArrIndex < sizeOfRight) {
char *ptrLeft, *ptrRight;
long gradeLeft = strtol(leftArr[leftArrIndex].grade, &ptrLeft, BASE_COUNT);
long gradeRight = strtol(rightArr[rightArrIndex].grade, &ptrRight, BASE_COUNT);
if (gradeLeft > gradeRight) {
mergedArr[mergedArrIndex] = rightArr[rightArrIndex];
rightArrIndex++;
} else {
mergedArr[mergedArrIndex] = leftArr[leftArrIndex];
leftArrIndex++;
}
mergedArrIndex++;
}
if (leftArrIndex == sizeOfLeft) {
for (int i = mergedArrIndex; i < (sizeOfLeft + sizeOfRight); i++) {
mergedArr[i] = rightArr[rightArrIndex];
rightArr++;
}
} else {
for (int i = mergedArrIndex; i < (sizeOfLeft + sizeOfRight); i++) {
mergedArr[i] = leftArr[leftArrIndex];
leftArr++;
}
}
}
void mergeSort(Student studentsArray[], int amountOfStudents) {
if (amountOfStudents <= 1) {
return;
}
int leftSize = (amountOfStudents / 2);
int rightSize = (amountOfStudents - leftSize);
Student leftArr[5500], rightArr[5500];
for (int i = 0; i < leftSize; i++) {
leftArr[i] = studentsArray[i];
}
for (int i = 0; i < rightSize; i++) {
rightArr[i] = studentsArray[i + leftSize];
}
mergeSort(leftArr, leftSize);
mergeSort(rightArr, rightSize);
merge(rightArr, leftArr, studentsArray, rightSize, leftSize);
}
Ok, I think this should do what you want. It assumes that Student and BASE_COUNT have been defined:
#include <stdlib.h>
#include <stdio.h>
void merge(Student studentsArr[],
int leftSize, int rightSize,
Student scratchArr[])
{
Student *leftArr = studentsArr;
Student *rightArr = studentsArr + leftSize;
int leftIx = 0, rightIx = 0, mergeIx = 0, ix;
while (leftIx < leftSize && rightIx < rightSize) {
long gradeLeft = strtol(leftArr[leftIx].grade, NULL, BASE_COUNT);
long gradeRight = strtol(rightArr[rightIx].grade, NULL, BASE_COUNT);
if (gradeLeft <= gradeRight) {
scratchArr[mergeIx++] = leftArr[leftIx++];
}
else {
scratchArr[mergeIx++] = rightArr[rightIx++];
}
}
while (leftIx < leftSize) {
scratchArr[mergeIx++] = leftArr[leftIx++];
}
// Copy the merged values from scratchArr back to studentsArr.
// The remaining values from rightArr (if any) are already in
// their proper place at the end of studentsArr, so we stop
// copying when we reach that point.
for (ix = 0; ix < mergeIx; ix++) {
studentsArr[ix] = scratchArr[ix];
}
}
void mergeSortInternal(Student studentsArray[],
int amountOfStudents,
Student scratchArr[])
{
if (amountOfStudents <= 1) {
return;
}
int leftSize = amountOfStudents / 2;
int rightSize = amountOfStudents - leftSize;
mergeSortInternal(studentsArray, leftSize, scratchArr);
mergeSortInternal(studentsArray + leftSize, rightSize, scratchArr);
merge(studentsArray, leftSize, rightSize, scratchArr);
}
#define MAX_ARR_SIZE 5500
void mergeSort(Student studentsArray[], int amountOfStudents)
{
if (amountOfStudents <= 1) {
return;
}
if (amountOfStudents > MAX_ARR_SIZE) {
fprintf(stderr, "Array too large to sort.\n");
return;
}
Student scratchArr[MAX_ARR_SIZE];
mergeSortInternal(studentsArray, amountOfStudents, scratchArr);
}
The top-level sort function is mergeSort, defined as in the original post. It declares a single scratch array of size MAX_ARR_SIZE, defined as 5500. The top-level function is not itself recursive, so this scratch array is only allocated once.

My BFS code is only showing the direct paths from source to destination, but not all possible paths

I want to print all possible paths from a given source and destination. But in my BFS code, it only shows the two paths, not the multiple path. For a directed graph where n = 4, edge = 6, given,
1-2
1-3
1-5
5-3
5-4
3-4
3-2
It should've printed 3 paths:
1-5-4
1-3-4
1-5-3-4
But it only shows this two paths
1-3-4
1-5-4
This is my sample code for finding the src to destination path
#include <stdio.h>
int queue1[100], state[100], parent[100];
int front = 0, rear = -1, maxSize = 100;
int count = 0;
int initial = 1, waiting = 2, visited = 3;
int n, e;
int adj[100][100];
bool isEmpty()
{
return count == 0;
}
bool isFull()
{
return count == maxSize;
}
void enqueue(int val)
{
if (!isFull())
{
if (rear == maxSize - 1)
{
rear = -1;
}
rear++;
queue1[rear] = val;
count++;
}
}
int dequeue()
{
int val = queue1[front];
front++;
if (front == maxSize)
{
front = 0;
}
count--;
return val;
}
void BFS_Traversal(int src, int des)
{
int done = 0;
enqueue(src);
state[src] = waiting;
parent[src] = -1;
printf("path ");
while (!isEmpty() && done == 0)
{
src = dequeue();
// printf("%d ",src);
state[src] = visited;
for (int i = 1; i <= n; i++)
{
if (adj[src][i] == 1 && state[i] == initial)
{
enqueue(i);
state[i] = waiting;
parent[i] = src;
if (i == des)
{
state[i] = initial;
int k = des;
do
{
printf("%d ", k);
k = parent[k];
} while (k != -1);
printf("\n");
}
}
}
}
}
int main()
{
int src, start, end, des;
scanf("%d%d", &n, &e);
for (int i = 1; i <= e; i++)
{
scanf("%d%d", &start, &end);
adj[start][end] = 1;
}
for (int i = 1; i <= n; i++)
{
state[i] = initial;
}
for (int k = 1; k <= n; k++)
{
parent[k] = -1;
}
scanf("%d%d", &src, &des);
BFS_Traversal(src, des);
}
As, you can see 1-5-3-4 path is not showing because they are already visited. How should I modify this code to print all possible paths?

How to remove item from array in C?

I want to remove element from b->array as shown in below code:removeItem function
I have tried to remove 12.12 from the b->array = {11.11,12.12,13.13}but it leads to segmentation fault instead of printing {11.11,13.13}. can anybody help to get rid of it?
typedef struct
{
float val;
}data;
typedef struct
{
data **array;
int size;
}bag;
int main(int argc,char* argv[])
{
bag *str = createBag();
#ifdef DEBUG
printf("Inital values: %p %d\n",str->array,str->size);
#endif
data *iptr = createData(10.10);
data *iptr1 = createData(11.11);
data *iptr2 = createData(12.12);
data *iptr3 = createData(13.13);
data *iptr4 = createData(14.14);
#ifdef DEBUG
printf("%f\n",iptr->val);
#endif
addData(str,iptr);
addData(str,iptr1);
addData(str,iptr2);
addData(str,iptr3);
addData(str,iptr4);
printBag(str);
data *ptr = getData(str,4);
printf("Data item 4 is:%f\n",ptr->val);
int b = getBagSize(str);
printf("Size of bag: %d\n",b);
removeBack(str);
printBag(str);
removeFront(str);
printBag(str);
//cleanBag(str);
int s = searchBag(str,10.10);
printf("%d\n",s);
removeItem(str,12.12);
printBag(str);
return 0;
}
bag* createBag()
{
bag *str = (bag*)malloc(sizeof(bag));
str->array = NULL;
str->size = 0;
return str;
}
data* createData(float v)
{
data *iptr = (data*)malloc(sizeof(data));
iptr->val = v;
return iptr;
}
void addData(bag* b, data* d)
{
b->size++;
data** array1 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array1[i] = b->array[i];
}
}
free(b->array);
array1[b->size-1] = d;
b->array = array1;
}
void printBag(bag *b)
{
int i;
for(i=0; i<b->size;i++)
{
printf("%f\n",b->array[i]->val);
}
}
data* getData(bag *b, int pos)
{
if(pos>5)
{
printf("change array position\n");
}
return b->array[pos];
}
int getBagSize(bag *b)
{
return b->size;
}
void removeBack(bag *b)
{
b->size--;
free(b->array[b->size]);
data **array2 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array2[i] = b->array[i];
#ifdef DEBUG
printf("%f\n",array2[i]->val);
#endif
}
free(b->array);
b->array = array2;
}
}
void removeFront(bag *b)
{
b->size--;
free(b->array[0]);
data **array2 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array2[i] = b->array[i+1];
#ifdef DEBUG
printf("%f\n",array2[i]->val);
#endif
}
free(b->array);
b->array = array2;
}
}
/*void cleanBag(bag *b)
{
int i;
for(i=0;i<b->size;i++)
{
free(b->array[i]);
}
free(b->array);
free(b);
}*/
int searchBag(bag *b,float v)
{
int i;
for(i=0;i<b->size;i++)
{
if(b->array[i]->val==v)
{
return (i+1);
}
}
return -1;
}
void removeItem(bag *b, float v)
{
int flag = 0,i = 0;
flag = searchBag(b,v);
if(flag != -1)
{
data **array2 = (data**)malloc(sizeof(data*)*(b->size-1));
for(i = 0; i < (b->size); i++)
{
if(i == (flag-1))
{
i = i + 1;
continue;
}
array2[i] = b->array[i];
}
free(b->array);
b->size--;
b->array = array2;
}
else
{
printf("Element is not found\n");
}
}
There are two mistakes in your code.
searchBag returns 0, if the float value is not found in the bag.
However, the function removeItem test for if(flag != -1). It should test for if(flag != 0) instead.
The reason for the seg. fault you encountered also lies in the removeItem function.
Note how the memory allocated for array2 is for b->size-1 elements only. Valid array indices for this array are in the range of 0 ... b->size-2.
Now, look at the for loop, and you will note that the index variable i will run from 0 to b->size-1. Exactly in the last iteration i will become b->size-1, so the code tries to write at the location array2[b->size-1] which is beyond the allocated memory of array2.
A possible fix of the removeItem function could look like this:
void removeItem(bag *b, float v)
{
int flag = 0;
int oldArrayIndex = 0;
int newArrayIndex = 0;
flag = searchBag(b,v);
if(flag != 0)
{
data **array2 = (data**) malloc(sizeof(data*)*(b->size-1));
for(oldArrayIndex = 0; oldArrayIndex < (b->size); oldArrayIndex++)
{
if(oldArrayIndex != (flag-1))
{
array2[newArrayIndex] = b->array[oldArrayIndex];
newArrayIndex++;
}
}
free(b->array);
b->size--;
b->array = array2;
}
else
{
printf("Element is not found\n");
}
}

Resources