There are 3 stacks - A, B, C
Stacks A and B are sorted (the number on the top of the stack is the biggest). Stack C is Empty Only 5 operation are allowed:
push, pop, top, is_empty, create
We need to write a function that receives the stacks A and B, moves all the numbers in stacks A and B to stack C and stack C must be sorted (biggest Number is on top).
I have the algorithm :
>
Compare top of A with top of B
Pop the least element and push to stack C
Repeat step 2 until any of the stack ( A or B) becomes empty
Move remaining elements from non-empty stack to C. Now you have all the elements in C but in ascending order. (That is least element at top).
Move all the elements from C to A. (Contents in A are in descending order)
Move all the elements from A to B. (Contents in B are in ascending order)
Move all the elements from B to C.
And here is the CODE:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_MEMBERS 4
typedef struct
{
int num;
}ITEM;
typedef struct
{
ITEM a[MAX_MEMBERS];
int top;
} STACK;
void create_stack(STACK *s)
{
s->top=-1;
}
int is_empty(STACK *s)
{
return s->top==-1;
}
int is_full(STACK *s)
{
return s->top==MAX_MEMBERS-1;
}
ITEM pop(STACK *s)
{
return s->a[s->top--];
}
void push(STACK *s,ITEM *item)
{
s->a[++s->top]=*item;
}
ITEM top(STACK *s)
{
return s->a[s->top];
}
void sort (STACK *a,STACK *b,STACK *c)
{
int i;
ITEM y,x;
while(!is_empty(a)||!is_empty(b))
{
y=top(a);
x=top(b);
if(&y>&x)
{
push(c,&x);
pop(b);
}
else
{
push(c,&y);
pop(a);
}
}
if(!is_empty(a))
{
while(!is_empty(a))
x=pop(a);
push(c,&x);
}
else
while(!is_empty(b))
{
x=pop(b);
push(c,&x);
}
while(!is_empty(c))
{
x=pop(c);
push(a,&x);
}
while(!is_empty(a))
{
x=pop(a);
push(b,&x);
}
while(!is_empty(b))
{
x=pop(b);
push(c,&x);
}
for(i=0;i<MAX_MEMBERS-1;i++)
printf("%d",c->a[i]);
}
void main(void)
{
int i;
STACK a,b,c;
ITEM x;
create_stack(&a);
create_stack(&b);
create_stack(&c);
for(i=0;i<4;i++)
{
printf("\nEnter a number to insert for A: ");
scanf("%d",&x.num);
push(&a,&x);
}
for(i=0;i<4;i++)
{
printf("\nEnter a number to insert for B: ");
scanf("%d",&x.num);
push(&b,&x);
}
sort(&a,&b,&c);
}
I debugged the code and saw where is the problem ..
it here : if(&y>&x)
It always give a "true" value for this boolean condition ..
even when x
Because you compare addresses of the variables, not variables itself
Related
i am trying to make a list that order integers in ascendant order during the insertation phase ,for some reason it gives me a wierd number after putting the list in the right order,can someone tell me where i messed up?or if you have any optimal version of this functionajout_liste_croissant()?
#include"liste.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void ajout_liste_croissant(element**l,int info)
{assert(*l);
element*nouv;
nouv=(element*)malloc(sizeof(element));
if(vide(*l))/*if list is empty will add the new info without comparing */
{
*l=nouv;
(*l)->cle=info;
(*l)->suivant=NULL;}
else
{element*courant,*q;
int inter;
courant=*l;
int trouve=0;
while(courant!=NULL&&!trouve)/*parcouring the list till the end or when finding a value bigger than the entered one*/
{if(info<courant->cle)
trouve=1;
else{q=courant;/*this pointer get the adress of the previous current position in the list*/
courant=courant->suivant;}}/*the current pointer move to next adress*/
if(trouve)
{inter=courant->cle;
courant->cle=info;/*exchanging values in case current positon is bigger than the entered value*/
info=inter;
nouv->cle=info;
nouv->suivant=courant->suivant;
courant->suivant=nouv;
}
else
/*if the list reaches end*/
{
nouv->cle=info;
q->suivant=nouv;/*adding the new element at the end of the list*/
nouv->suivant=NULL;}
}
}
void afficher(element*l)
{assert(!vide(l));
while(l!=NULL)
{printf("%d\n",l->cle);
l=l->suivant;}
}
There are not too many errors. But your code is absolutely unreadable. I do not know how you could read it and, therefore, debug it.
So, I added spaces, indentations, new lines ... and it works with minor changes. I let you discover these changes. This answer is not necessarily the best way to do it but, starting from your code, I tried to do the minimum of changes. Moreover, avoid French (text, function names, ...) in your code when you ask question on stackoverflow.
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct element {
int cle;
struct element *suivant;
} element;
void creer_liste(element **l) {
assert(l);
*l=NULL;
}
int vide(element *l) {
return(l==NULL);
}
void ajout_liste_croissant(element **l,int info) {
assert(l);
element *nouv;
nouv=(element*)malloc(sizeof(element));
if( vide(*l) ) {
*l=nouv;
(*l)->cle=info;
(*l)->suivant=NULL;
} else {
element *courant,*q;
int inter;
courant=*l;
int trouve=0;
while ( courant != NULL && !trouve ) {
if(info<courant->cle)
trouve=1;
else {
q=courant;
courant=courant->suivant;
}
}
if(trouve) {
inter=courant->cle;
courant->cle=info;
info=inter;
nouv->cle=info;
nouv->suivant=courant->suivant;
courant->suivant=nouv;
} else {
nouv->cle=info;
q->suivant=nouv;
nouv->suivant=NULL;
}
}
}
void afficher(element *l) {
assert(!vide(l));
while(l!=NULL) {
printf("%d\n",l->cle);
l=l->suivant;
}
}
int main() {
element *data;
int nb,info;
creer_liste(&data);
printf("donner la taille de la liste:\n");
scanf("%d",&nb);
for(int i=0;i<nb;i++) {
printf("donner un entier:\n");
scanf("%d",&info);
ajout_liste_croissant(&data,info);
}
printf("_____________________\n");
afficher(data);
}
I've been working on creating my own GUI library for MS-DOS on my free time and I got stuck on how I can implement an array that would contain structures of GUI elements.
So far I was able to make it draw the window itself, but I needed a way to draw the elements inside the window such as text boxes, text labels, buttons, ect.
Here's my current code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>
#include "graph.h" //Watcom graphics library
#define false 0
#define true 1
#define border_none 0
#define border_out 1
#define border_in 2
struct text_button {
char text[128];
int pos_x;
int pos_y;
int size_x;
int size_y;
int text_color;
int button_color;
};
struct window_structure {
char title[128];
int pos_x;
int pos_y;
int pre_pos_x;
int pre_pos_y;
int size_x;
int size_y;
int min_size_x;
int min_size_y;
int max_size_x;
int max_size_y;
int show_tab;
int border_type;
int focused;
//Right here is where I would add the array containing the elements.
};
void draw_border(int type,int pos_x,int pos_y,int size_x,int size_y) {
int c_1,c_2;
if (type==1) {
c_1=15;
c_2=0;
} else if (type==2) {
c_1=0;
c_2=15;
}
if (type!=0) {
_setcolor(c_1);
_moveto(pos_x,pos_y);
_lineto(pos_x+size_x,pos_y);
_moveto(pos_x,pos_y);
_lineto(pos_x,pos_y+size_y);
_setcolor(c_2);
_moveto(pos_x+size_x,pos_y+size_y);
_lineto(pos_x+size_x,pos_y);
_moveto(pos_x+size_x,pos_y+size_y);
_lineto(pos_x,pos_y+size_y);
}
}
void draw_box(int type,int color,int pos_x,int pos_y,int size_x,int size_y) {
_setcolor(color);
_rectangle(_GFILLINTERIOR,pos_x,pos_y,pos_x+size_x,pos_y+size_y);
draw_border(type,pos_x-1,pos_y-1,size_x+2,size_y+2);
}
struct window_structure create_window(
char title[],
int pos_x,
int pos_y,
int size_x,
int size_y,
int min_size_x,
int min_size_y,
int max_size_x,
int max_size_y,
int show_tab,
int border_type
) {
struct window_structure window;
strcpy(window.title,title);
window.pos_x=pos_x;
window.pos_y=pos_y;
window.pre_pos_x=pos_x;
window.pre_pos_y=pos_y;
window.size_x=size_x;
window.size_y=size_y;
window.min_size_x=min_size_x;
window.min_size_y=min_size_y;
window.max_size_x=max_size_x;
window.max_size_y=max_size_y;
window.show_tab=show_tab;
window.border_type=border_type;
window.focused=true;
return window;
}
void draw_window(struct window_structure window) {
int offset_x,offset_y;
if (window.size_x<window.min_size_x) {
window.size_x=window.min_size_x;
} else if (window.size_x>window.max_size_x) {
window.size_x=window.max_size_x;
}
if (window.size_y<window.min_size_y) {
window.size_y=window.min_size_y;
} else if (window.size_y>window.max_size_y) {
window.size_y=window.max_size_y;
}
if (window.show_tab==true) {
int tab_color;
if (window.focused==true) {
tab_color=9;
} else {
tab_color=8;
}
draw_box(
window.border_type,
tab_color,
window.pos_x,
window.pos_y-1,
window.size_x-1,
18
);
offset_x=0;
offset_y=20;
}
draw_box(
window.border_type,
7,
window.pos_x+offset_x,
window.pos_y+offset_y,
window.size_x-1,
window.size_y-1
);
//Once the window has been drawn, the next part it would do here is draw the elements
window.pre_pos_x=window.pos_x;
window.pre_pos_y=window.pos_y;
}
I know MS-DOS is quite outdated, this is just for my hobby. I'm currently using Open Watcom as my compiler.
//Right here is where I would add the array containing the elements.
You know, since you'll have a variable number of elements, you can't declare a fixed-size array here, so you can just declare a pointer and allocate the array as needed. You'll also need to store the number of elements allocated.
struct window_structure
{
…
int nelem; // how many elements are there
struct element *elements; // pointer to allocated elements
};
Both shall be initialized to 0.
struct window_structure create_window(…)
{
…
window.nelem = 0;
window.elements = NULL;
return window;
}
The struct element type could be defined as
struct element
{ enum elemtype { text_button, /* add other types here */ } elemtype;
union
{ struct text_button tb;
/* add other types here */
} u;
};
An element, e. g. a text_button, could then be added to the window with
struct element *new;
new = realloc(window.elements, (window.nelem+1) * sizeof *new);
if (!new) exit(1); // or some better error handling
window.elements = new;
window.elements[window.nelem].elemtype = text_button;
window.elements[window.nelem].u.tb = your_text_button_to_add;
++window.nelem;
//Once the window has been drawn, the next part it would do here is draw the elements
This would then be done like
int i;
for (i = 0; i < window.nelem; ++i)
switch (window.elements[i].elemtype)
{
case text_button:
/* draw the text_button window.elements[i].u.tb here */
break;
/* add cases for other element types here */
}
I am trying sort a number in a linked list from small to big.
But its not working !
the debugger says there are a problem when i put the second number into the
list ( in main )but i dont know why .
Any help ?
#include<stdio.h>
#include<stdlib.h>
typedef struct list list;
struct list{
int a;
list *nxt;
};
void sort(list *l){
int temp,tp;
list *AIDE,*k;
k=AIDE=(list*)malloc(sizeof(list));
while (l->nxt!= NULL)
{
while (l->nxt->a < l->a)
{
temp=l->a;
l=l->nxt;
l->nxt->a=temp;
l=l->nxt;
while (l->a < AIDE->nxt->a )
{
tp=AIDE->a;
AIDE->a=l->a;
AIDE->nxt->a=tp;
AIDE=AIDE->nxt;
}
}
l=l->nxt;
}
while (k->nxt!= NULL)
{
l->a=k->a;
l=l->nxt;
k=k->nxt;
}
l->nxt=NULL;
}
int main() {
list *t,*s;
int n,i,c=0;
printf("\n how many number you need to enter? ");
scanf("%d",&n);
s=t=(list*)malloc(sizeof(list)*n);
while (c!=n)
{
printf("\n Donner le nb %d :",c+1);
scanf("%d",&t->a);
t=t->nxt;
c++;
}
t->nxt=NULL;
sort(s);
while (t->nxt!=NULL)
{
printf("%d",t->a);
}
return 0;
}
In the loop where you have the problem, what do you think the expression t=t->nxt would do?
When you enter the loop, t is pointing to allocated but uninitialized memory, therefore dereferencing e.g. t->nxt will lead to undefined behavior.
A simple solution would be to e.g. do
t->nxt = t++ + 1;
I'm faced with a problem which I've been unable to tackle for quite some time.
I've been given a graph as follows,in a M x N matrix:
2 2
a b
a c
Note
I've interpreted the graph above as a matrix,only consisting of non-diagonal edges.
Here the first line represents values of M and N respectively.
The graph is only connected either along vertical,or adjacent direction,i.e.,up,down,left and right. diagonal edges not present.
In order to find the adjacency list of the graph(the desired output here):
a-b-c
b-a-c
c-a-b
Steps followed by me in the code:
1.Read M x N matrix into a 2D array.
2.Created a list of unique vertices of the graph as Unode[arrmax].
3.For each element of the matrix,if the character matches with an element of the unique vertices list,I've called the modify Adjacency List procedure that searches the neighbours of the concerned matrix vertex and populates/appends to the the Adjacency list if distinct nodes are found.
It takes as arguments, i,j,M,N,AdjList,number of elements in the list and makes the changes.
5.I've kept the list of nodes to be global for easy modification.
6.Next I intend to use the adjacency list produced to use in DFS procedure and find the DFS forest.
The Problem statement:
the input consists of a grid of size M X N. Each cell in the grid
contain a lower case letter of the English alphabet.In a natural way,
the cells are of two types: boundary cells and internal cells. Each
internal cell in the grid has four neighbours in one of the left,
right, top, down directions. A string of characters can be formed by
starting at any cell and traversing the grid through the neighbours.
You have to print all the possible strings subject to the following
constraints:
**No two characters in a string can be same
**No two strings can be same in the final output
**The strings should be printed in alphabetically sorted order.
INPUT:
First line contains two integers M and N
Next M lines contains N space separated characters each
OUTPUT:
Print all possible strings in sorted order and obeying the above constraints.
INPUT SIZE:
1 <= M, N <= 20
SAMPLE INPUT:
2 2
a b
a c
SAMPLE OUTPUT:
a ab abc ac acb b ba bc bca c ca cb cba
[UPDATE]:
Completely redesigned the code,used structures for the graph nodes,and one for handling indices.
Yet the result I'm getting:
a--b-a
b--a
a
c--a
My code[Relevant Portion]:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define ADJMAX 20
#define arrmax 400
typedef struct uniq_node{
char ch;
char AdjList[ADJMAX];
int numofelem;
int visited;
}unode;
unode Ulist[arrmax];
int uniq_tot=0;
typedef struct index
{
int i,j;
}Ind;
Ind indx;
int charcomp(char sch,char arr[],int arrlim);
void adjModify(unode*,char*,int,int,Ind);
int chIndex(int,int,int,int);
int main(void) {
int mvar,nvar;
char str[15],*token;
long integer;
/*To scan the values of M & N*/
scanf("%d %d\n",&mvar,&nvar);
int iter,iterv,jterv;
/*To create the character matrix of M x N*/
char cmat[mvar][nvar];
/*Initializing the unique nodes list*/
/*To read-in the matrix from the stdin:-A LOT OF HARD WORK*/
for(iterv=0;iterv<mvar;iterv++)
{
fgets(str,50,stdin);
jterv=0;
token=strtok(str," ");
while(token)
{
/*Assigning value to the character matrix*/
cmat[iterv][jterv]=*token;
/*Code to populate the list of unique elements*/
if(charcomp(*token,Ulist[uniq_tot].AdjList,uniq_tot)==3)
{
Ulist[uniq_tot].ch=*token;
uniq_tot++;
Ulist[uniq_tot].numofelem=1;
Ulist[uniq_tot].AdjList[0]=*token;
//Ulist[uniq_tot].visited=0;
}
jterv++;
token = strtok(NULL, " ");
}
}
/*To populate the adjacency lists */
char ch;
for(iterv=0;iterv<mvar;iterv++)
{
for(jterv=0;jterv<nvar;jterv++)
{
ch=cmat[iterv][jterv];
indx.i=iterv;
indx.j=jterv;
for(iter=0;iter<uniq_tot;iter++)
{
if(ch==Ulist[iter].ch)
break;
}
adjModify(&Ulist[iter],(char*)cmat,mvar,nvar,indx);
}
}
/*for(iter=0;iter<uniq_tot;iter++)
{
printf("%c",Ulist[iter].ch);
printf("\n%s\n",Ulist[iter].AdjList);
for(iterv=0;iterv<Ulist[iter].numofelem;iterv++)
{
printf("-%c",Ulist[iter].AdjList[iterv]);
}
printf("\n");
}*/
return 0;
}
int chIndex(int i,int j,int mvar,int nvar)
{
return (i>=0 && i<mvar && j>=0 && j<nvar);
}
void adjModify(unode* Unode,char* mat,int mvar,int nvar,Ind mind)
{
int idum,jdum;
if(chIndex(mind.i,mind.j-1,mvar,nvar))
{
idum=mind.i;
jdum=mind.j-1;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
if(chIndex(mind.i,mind.j+1,mvar,nvar))
{
idum=mind.i;
jdum=mind.j+1;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
if(chIndex(mind.i-1,mind.j,mvar,nvar))
{
idum=mind.i-1;
jdum=mind.j;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
if(chIndex(mind.i+1,mind.j,mvar,nvar))
{
idum=mind.i+1;
jdum=mind.j;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
}
/*Comparison routine*/
int charcomp(char fchar,char arr[],int ucindex)
{
int ivar;
for(ivar=0;ivar<ucindex;ivar++)
{
if(arr[ivar]==fchar)
return;
}
return 3;
}
I think you can skip creating individual nodes for every element in the 2D array. Having the 2D array implies a structured connectivity. When it starts getting large, traversing all these elements may become cumbersome.
My recommended approach would be the following:
Scan of the matrix and pull unique nodes. i.e. start with a scan and have the simple list a,b,c (you'll need to sort them).
Create a struct for each unique node consisting the number of paths you currently have and an array of char arrays to store each one in. i.e. char** myArray={{a},{ab},{abc},{ac},{acb}} would be the one for a (This is of course unknown when you start).
Loop through your unique nodes, and one by one find the location in the 2D array. Don't save them, just go through them one by one and do a scan function to look for all their paths.
The scan function should be recursive so it can go as far as it needs to while checking every possible path (recursive will help you check every direction at every node you traverse). Keep track of where you've been, and at ever step check that you have not already encountered that character.
When you can't go any further, make sure the string has not already been included, if it has continue to the next path, if not add it to the list.
this is my code in c++ without any library that can work in c but you just have to use in c printf instead of cout and instead of class use struct that's all. I also write code for breadth first traversal see below.
and include the header file also
// #include <stdio.h>
//#include<stdlib.h>
#include<iostream
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data=data;
this->next=NULL;
// cout<<"from node file"<<endl;
}
};
class Queue {
Node * head;
Node * tail;
int length;
public:
Queue() {
head=NULL;
tail=NULL;
length=0;
}
bool isEmpty() {
return length==0;
}
int size() {
return length;
}
int front() {
if(head==NULL) {
cout<<"Empty Queue"<<endl;
return 0;
}
return head->data;
}
void enqueue(int element) {
Node * newNode =new Node(element);
if(head==NULL) {
head=newNode;
tail=newNode;
}else{
tail->next=newNode;
tail=newNode;
}
length++;
}
int dequeue() {
if(head==NULL) {
cout<<"Empty queue"<<endl;
return 0;
}
int output= head->data;
Node * temp=head;
head=head->next;
temp->next=NULL;
delete temp;
length--;
return output;
}
};
class AdjList{
public:
Node * head;
AdjList() {
head=NULL;
//cout<<"from adlist"<<endl;
}
void add (int data) {
Node * newNode=new Node(data);
if(head==NULL) {
head=newNode;
}else {
Node* temp=head;
while(temp->next!=NULL) {
temp=temp->next;
}
temp->next=newNode;
}
}
};
class Graph{
public:
int v;
AdjList* adjList;
Graph(int v) {
this->v=v;
adjList=new AdjList[v];
}
void addEdge(int src, int dest) {
adjList[src].add(dest);
///for bidrectional add below code
//adjList[dest].add(src);
}
void print(){
for(int i=0;i<v;i++){
Node *temp = adjList[i].head;
cout << i << " -> ";
while(temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}
void bfs(int src) {
// using a queue also in this file how to add queue structure
Queue q;
bool* visited=new bool [v]{0};
q.enqueue(src);
visited[src]=true;
while(!q.isEmpty()) {
int node= q.front();
cout<<node<<" ";
q.dequeue();
Node *temp = adjList[node].head;
while(temp!=NULL){
if(!visited[temp->data]) {
q.enqueue(temp->data);
visited[temp->data]=true;
}
// cout<<"data "<<temp->data;
temp=temp->next;
/// how to traverse
}
}
}
};
int main(){
Graph g(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(2, 3);
g.addEdge(3,4);
g.addEdge(4,5);
g.bfs(0);
// g.print();
return 0;
}
I want to try insertion function for linked list. Function holds the record array and link array.for example;
head=0
string[0]="Angel" linked[0]=1
string[1]="Cesar" linked[1]=2
string[2]="Eduardo" linked[2]=3
string[3]="Pamela" linked[3]=-1/*-1 element does not show*/
head is Angel and Angel shows Eduardo.e.g. If you add element Denial,string[4]=Denial linked[4]=2 Denial shows Eduardo.Attention :---->>string[1]="Cesar" linked[1]=4 link array is updated.
I am getting some errors in this function.(element can not be added.) a little help
#define SIZE 10
int main()
{
int linked[SIZE]={3,0,4,-1,1};
char *str[]={"Ellian","Calanthe","Adela","Gardenia","Barbara",NULL};
return 0;
}
void list(int arr[],int head,int linky[]){
int adr=head;
while(adr!=-1){
puts(arr[adr]);
adr=linky[adr];
}
}
void insert(int arrr[],int head,int linky[],char element){
int k,N=0,prev,next;
for(k=0;arrr[k]<NULL;k++)
N++;
arrr[N]=element;
if(element>arrr[head])
{
prev=head;
next=linky[head];
while((next!=-1) && (arrr[next]<element)){
prev=next;
next=linky[next];
}
linky[prev]=N;
linky[N]=next;
}
else{
linky=head;
head=N;
}
N++;
listele(arr,N,linky);
}
replace
#define SIZE 10
by
int size = 10;
and replace
void insert(char str[],int link[],int size,int first)
by
void insert(char str[],int link[],int& size,int& first)
to start of. Still might not work but atleast now the things you do actually do what you want them to do
You are using your stringarray contents before initializing them, to avoid undefined behaviour change this:
char string[SIZE];
to this:
char string[SIZE] = {0};
If you want your string array to start with zeroes inside.
Also notice you are running past the last element of your arrays when you do:
link[size] = ...
as you array is of exactly size elements then the max index you can use is size - 1.