Related
I don't understand if both of the following definitions of a priority queue are correct:
1.
-ascending priority queue - elements are inserted arbitrarily, but after deletion, the smallest element is removed (assuming that data is an integer).
-descending priority queue - elements are inserted arbitrarily, but after deletion, the largest element is removed (assuming that data is an integer).
Examples for each:
5 15 10 -> after dequeue() -> 15 10
15 5 10 -> after dequeue() -> 5 10
2.
Every element of priority queue has a priority by which deletion is done.
There can be two cases. First, element with the highest priority is removed. Second, element with the lowest priority is removed.
Clearly, this is different from the first definition. If we assign priorities 6,3,12 to numbers 15, 10, 5, then after dequeue() operation there are two cases. If element with the lowest priority is removed, then the queue is 15,5 (10 is removed). If element with the highest priority is removed, then the queue is 15,10 (5 is removed).
Also, if element of a queue aren't numbers (strings, for example), then the first definition is useless.
Is that correct?
Question: Are both definitions correct? It seems to me that the first is only usable for numbers but even then it violates the priority from the second definition. Could someone explain this?
Here are two implementations for both definitions in C:
//1. DEFINITION//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int itemCount = 0;
int peek(){
return intArray[itemCount - 1];
}
bool isEmpty(){
return itemCount == 0;
}
bool isFull(){
return itemCount == MAX;
}
int size(){
return itemCount;
}
void insert(int data){
int i = 0;
if(!isFull()){
// if queue is empty, insert the data
if(itemCount == 0){
intArray[itemCount++] = data;
}else{
// start from the right end of the queue
for(i = itemCount - 1; i >= 0; i-- ){
// if data is larger, shift existing item to right end
if(data > intArray[i]){
intArray[i+1] = intArray[i];
}else{
break;
}
}
// insert the data
intArray[i+1] = data;
itemCount++;
}
}
}
int removeData(){
return intArray[--itemCount];
}
int main() {
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
int num = removeData();
printf("Element removed: %d\n",num);
return 0;
}
//2. DEFINITION//
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5 /* Size of Queue */
int f=0,r=-1; /* Global declarations */
typedef struct PRQ
{
int ele;
int pr;
}PriorityQ;
PriorityQ PQ[SIZE];
PQinsert(int elem, int pre)
{
int i; /* Function for Insert operation */
if( Qfull()) printf("\n\n Overflow!!!!\n\n");
else
{
i=r;
++r;
while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1].ele=elem;
PQ[i+1].pr=pre;
}
}
PriorityQ PQdelete()
{ /* Function for Delete operation */
PriorityQ p;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
p.ele=-1;p.pr=-1;
return(p); }
else
{
p=PQ[f];
f=f+1;
return(p);
}
}
int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}
int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}
display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("[%d,%d] ",PQ[i].ele,PQ[i].pr);
printf("<-Rear");
}
}
main()
{ /* Main Program */
int opn;
PriorityQ p;
do
{
printf("\n ### Priority Queue Operations(DSC order) ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element and its Priority?");
scanf("%d%d",&p.ele,&p.pr);
PQinsert(p.ele,p.pr); break;
case 2: p=PQdelete();
if( p.ele != -1)
printf("\n\nDeleted Element is %d \n",p.ele);
break;
case 3: printf("\n\nStatus of Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
A priority queue is a data structure holding elements (like any data structure) as well as their priority. This is your second definition.
However, in some cases, the elements actually represent their own priority. This is your first definition : sometimes, you just need to store a bunch of unordered numbers and retrieve them in order. Note that in this case, elements are not necessarily numbers. Other data types might have a property that can be used as priority.
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 could really use some help with an address book program I've been struggling on for days now. I'm working with a doubly linked list in C. I'm trying to add nodes into the list at user-entered positions, starting with position 0. The positions will not be entered out of range. (no inserts at position 1 before something at position 0 etc.) The positions can be repeated though: inserting the new node in the position before the previous position occupant. (for example: if position 1 has x, and new node is inserted at position 1 with y, position 1 now has y and position 2 has x)
I need to take the user entered position number and retrieve the current person in that position, but I can't quite get it right. Also, I have included my insert function if you wanted to take a look at that as well because it isn't working properly either. Thanks for any help!
EDIT: The main problem right now is that my code for finding pPersonCur is failing when position == 1. Also, the insert function is not entering things in the proper order (the newest insertion in a position does not displace the older insertion correctly). The broken pPersonCur code makes it hard to diagnose why exactly this is, however.
addressbook.h excerpt:
typedef struct person Person;
struct person {
char lastName[255];
char firstName[255];
char email[255];
char phoneNumber[255];
Person *pNext;
Person *pPrev;
};
addressbook.c excerpt:
#include "addressbook.h"
Person * InsertPerson(Person * pPersonCur) {
Person * pPersonNew;
/* data gathered for CreatePerson() function here */
pPersonNew = CreatePerson(pLastName, pFirstName, pEmail, pPhoneNumber);
if (pPersonCur)
{
pPersonNew->pNext = pPersonCur;
pPersonNew->pPrev = pPersonCur->pPrev;
pPersonCur->pPrev = pPersonNew;
if (pPersonNew->pPrev)
pPersonNew->pPrev->pNext = pPersonNew;
} else
{
pPersonNew->pPrev = pFirst;
pPersonNew->pNext = NULL;
if (pFirst)
pFirst->pNext = pPersonNew;
}
return (pPersonNew);
}
main.c excerpt:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "addressbook.h"
Person *pFirst; /* First name in list */
int main(void) {
Person *pPersonCur = NULL; /* current Person */
int bDone = 0, position = 0, counter = 0;
pFirst = NULL;
printf("Ready\n");
while (!bDone) {
char input = getchar();
switch (input) {
case 'a':
counter = 0;
scanf("%d", &position); /* Where desired position is entered */
if (position == 0) {
if (pFirst) {
if (pFirst->pNext) {
pPersonCur = pFirst->pNext;
}
} else {
pPersonCur = pFirst;
}
} else {
pPersonCur = pFirst->pNext;
while (counter < position) {
pPersonCur = pPersonCur->pNext;
counter++;
}
}
InsertPerson(pPersonCur); /* Takes in person at desired position, return value is new inserted person */
break;
/* Some other cases here */
case 'q':
bDone = 1;
break;
}
}
/* Rest of code */
It seems so that you never assign a value to pFirst.
When position is not 0 the line pPersonCur = pFirst->pNext; is executed and pFirst in this place is still NULL.
Add a condition to your insert function to check whether list's head is assigned.
Person * InsertPerson(Person * pPersonCur) {
. . .
else
{
pPersonNew->pPrev = pFirst;
pPersonNew->pNext = NULL;
if (pFirst)
pFirst->pNext = pPersonNew;
else
pFirst = pPersonNew; // If pFirst is not assigned, assign it to newly created person
}
return (pPersonNew);
}
Despite that, if you happen to call InsertPerson with NULL argument, your code would put new Person after the first one and cut the rest of the list off.
To put new Person to the end of the list when called with NULL you could use something like this in your InsertPerson function:
if(pFirst) {
Person *last = pFirst;
while(last->pNext != NULL) {
last = last->pNext;
}
last->pNext = pPersonNew;
pPersonNew->pPrev = last;
}
else
pFirst = pPersonNew;
Insertion according to position index might fail as well if you give a position index that is higher than there are nodes in the list. Some sort of safety check should be added as well.
pPersonCur = pFirst->pNext;
while (counter < position && pPersonCur->pNext != NULL) { // If last node reached, stop the loop
pPersonCur = pPersonCur->pNext;
counter++;
}
This implementation would add new Person to the end of the list if position index is too high.
I would like to implement functions where I perform a pre, in and postorder traversal of an existing binary tree.
these traversals should then be displayed by a predefined test function
here's what i got so far for the preorder traversal
uint64_t i = 0;
int *binarytree_prefix(binarytree *tree) {
uint64_t *prefixArray = malloc(inputArrayLength_helper * sizeof(uint64_t));
prefixArray[i] = tree->value;
i++;
if (tree->left != NULL) {
return (binarytree_prefix(tree->left));
}
if (tree->right != NULL) {
return (binarytree_prefix(tree->right));
}
}
what I thought about it that it would insert the value of the current node into the array and then increent the position within the array and do a recursion on the left and then the right tree
however this does not work.
i hope someone is able to help me to make it running
What i did was a depth first search with a preorder traversal and then included the array to fill it with the current value
test function within main:
int *prefixArray = bintree_prefix(tree);
printf("Prefix notation : ");
for(uint64_t i = 0; i < inputArrayLength; i++) {
printf(" %d", prefixArray[i]);
}
printf("\n");
free(prefixArray);
ok after a few different variations of the code i finally got the right solution
for those interested
int *bintree_prefix(bintree *tree)
{
int *prefixArray = malloc(17*sizeof(uint64_t));
return (bintree_prefix_visited(tree, prefixArray));
}
int bintree_prefix_visited(bintree *tree, int *prefixArray)
{
if (tree!=NULL)
{
prefixArray[a]=tree->value;
a++;
bintree_prefix_visited(tree->left, prefixArray);
bintree_prefix_visited(tree->right, prefixArray);
}
return prefixArray;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
What my program do..
read a text file of format
store name 1
itemcode quantity
itemcode quantity
.
.
store name 2
itemcode quantity
itemcode quantity
.
.
When you Run my code you will Ask to Enter a task.
there are three options
L itemcode quantity
entering the above sequence will print all the stores which contains that item with the given quantity.
U itemcode quantity storename
this option takes three arguments itemcode int quantity and storename
the Function for this option just update the given store with the amount quantity.
Q
this option call my Savefile method which save the current data structure back to the file.
Problem.
There is a problem I am facing.
whenever I update file it updates successfully but when Enter Command Q to quit and save it doesn't save correctly..
save_file(char *)
it lost whole data just the first store is save..
stores.txt
carrefour_Milan
12345678 12
23456766 16
carrefour_Torino
12345678 65
67676765 12
Carrefour_Vercelli
23456766 20
and also can you help me in finding the time complexity of
int listfile(char *)
and
int updatefile(char *,int ,char *)
I mean Big O.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_ITEM 1000
#define MAXS 129
#define MAXL 132
#define MAXC 9
FILE *fp;
typedef struct store{
char Storename[MAXS];
int quantity;
struct store *NEXT;
}STORE;
typedef struct item{
char item_code[MAXC];
struct store *Stores;
struct item *NEXT;
}ITEM;
ITEM *list_item[MAX_ITEM];
int readfile(char *fname);
int update_file(char *item_code,int qty,char *name);
int hash(char *item_code);
int save_file(char *fname);
void init();
void init(){
int i;
for( i=0;i<MAX_ITEM;i++)
list_item[i]=NULL;
}
int readfile(char *fname){
char *p,line[MAXL+1],storen[MAXL+1];
int pos;
ITEM *current=NULL,*prev=NULL;
STORE *s_cur=NULL,*s_prev=NULL;
char itemcode[MAXC];int qty;
if((fp=fopen(fname,"r"))==NULL)
return -1;
while(!feof(fp)){
if(fgets(line,MAXL+1,fp)==NULL)
break;
if((p=strchr(line,'\n'))==NULL)
;
else
*p='\0';
if(line[0]>='a' && line[0]<='z' ||line[0]>='A' && line[0]<='Z')
strcpy(storen,line);
else{
//fgets(line,MAXL,fp);
if(sscanf(line,"%s %d",itemcode,&qty)>0){
current=(ITEM *)malloc(sizeof(ITEM));
if(current==NULL)
return -1;
pos=hash(itemcode);
if(list_item[pos]==NULL){
list_item[pos]=current;
if((s_cur=(STORE *)malloc(sizeof(STORE)))==NULL)
return -1;
strcpy(s_cur->Storename,storen);
strcpy(current->item_code,itemcode);
s_cur->quantity=qty;
current->Stores=s_cur;
s_cur->NEXT=NULL;
current->NEXT=NULL;
}
else{
ITEM *q=list_item[pos];
if((s_cur=(STORE *)malloc(sizeof(STORE)))==NULL)
return -1;
while(q!=NULL){
if(strcmp(q->item_code,itemcode)==0){
STORE *temp=q->Stores,*temp_a=NULL;
if(temp==NULL){
q->Stores=s_cur;
strcpy(s_cur->Storename,storen);
s_cur->quantity=qty;
s_cur->NEXT=NULL;
}
else{
while(temp!=NULL){
temp_a=temp;
temp=temp->NEXT;
}
temp_a->NEXT=s_cur;
strcpy(s_cur->Storename,storen);
s_cur->quantity=qty;
s_cur->NEXT=NULL;
}
}
q=q->NEXT;
}
if(q==NULL){
q=current;
current->NEXT=NULL;
current->Stores=s_cur;
strcpy(s_cur->Storename,storen);
s_cur->quantity=qty;
s_cur->NEXT=NULL;
}
}
}
}
}
fclose(fp);
return 0;
}
int listfile(char *item_code,int qty){
int i;
ITEM *u=NULL;
item_code[strlen(item_code)]='\0';
if(list_item[hash(item_code)]==NULL)
return -1;
else{
u=list_item[hash(item_code)];
while(u!=NULL){
if(strcmp(u->item_code,item_code)==0){
STORE *temp=u->Stores;
while(temp!=NULL){
if(temp->quantity>=qty){
printf("STORE %s\n",temp->Storename);
}
temp=temp->NEXT;
}
}
u=u->NEXT;
}
}
return 0;
}
int update_file(char *item_code,int qty,char *name){
ITEM *u=NULL;
item_code[strlen(item_code)]='\0';
name[strlen(name)]='\0';
if(list_item[hash(item_code)]==NULL)
return -1;
u=list_item[hash(item_code)];
if(u==NULL)
return -1;
while(u!=NULL){
if(strcmp(u->item_code,item_code)==0){
STORE *temp=u->Stores;
while(temp!=NULL){
if(strcmp(temp->Storename,name)==0)
temp->quantity+=qty;
temp=temp->NEXT;
}
}
u=u->NEXT;
}
return 0;
}
int hash(char *item_code){
int sum=0,s=0;
while(item_code[s]!='\0'){
sum+=33*item_code[s];
s++;}
return sum%MAX_ITEM;
}
void clear(){
char c;
while(c!='\n')
scanf("%c",&c);
}
main(){
int y;
char fname[]="stores.txt",line[MAXL],command,z[MAXS];
char x[MAXC];
init();
if(readfile(fname)==-1)
printf("Error reading file!");
else{
do{
printf("Enter task:");
fgets(line,MAXL,stdin);
sscanf(line,"%c",&command);
switch(command){
case 'L': sscanf(line,"%c%s%d",&command,x,&y);
if(listfile(x,y)==-1)
printf("No items were found\n");
break;
case 'U':sscanf(line,"%c%s%d%s",&command,x,&y,z);
if(update_file(x,y,z)==0)
printf("Update OK\n");
else
printf("Error when updating\n");
break;
case 'Q':if(save_file(fname)==0)
printf("Done\n!");
break;
default:printf("Enter correct command\n");
break;
}
}while(command!='Q');
}
}
int save_file(char *fname){
ITEM *p=NULL,*q=NULL;
int num=0,i,j;
char str[MAXS];
if((fp=fopen(fname,"w"))==NULL)
return -1;
for( i=0;i<MAX_ITEM;i++){
if(list_item[i]==NULL)
;
else{
p=list_item[i];
while(p!=NULL){
STORE *s=p->Stores;
if(s==NULL)
;
else{
if(strcmp(s->Storename,"0000\0")!=0){
strcpy(str,s->Storename);
// puts(str);
fprintf(fp,"%s\n",str);
}
while(s!=NULL){
for( j=0;j<MAX_ITEM;j++){
if(list_item[j]==NULL)
;
else{
q=list_item[j];
while(q!=NULL){
STORE *st=q->Stores;
if(st==NULL)
;
else{
while(st!=NULL){
if(strcmp(st->Storename,str)==0 && strcmp(st->Storename,"0000\0")!=0){
printf("%s %d\n",q->item_code,st->quantity);
fprintf(fp,"%s %d\n",q->item_code,st->quantity);
strcpy(st->Storename,"0000\0");
}
st=st->NEXT;
}
}
q=q->NEXT;
}
}
}
s=s->NEXT;
}
}
p=p->NEXT;
}
}
}
fclose(fp);
return 0;
}
This is an inconsistent and unreadable mess. I suggest as first steps to refactor the layout.
Repair the indentation so it reflects the code structure. Chose a bracing style and use it consistently. Something like this
if(x){
;
}else{
foo();
}
should better look like this:
if (x) {
;
}
else {
foo();
}
That's a much better starting point for any debugging and maintenance. And there is a lot of maintenance necessary.
Your code is very inefficient. For example when reading the file, you malloc the store structure separately in both branches of the if statement, and copy the store name in three different places, again in all different code paths. Why not simply malloc the store structure and initialise it correctly before you work out where to put it?
Also in the read file function, if the hash table position corresponding to the item is not empty, the memory allocated to "current" gets leaked.
Furthermore, if you actually find a match for the item, you don't break out of the loop which means that the block of code beginning:
if(q==NULL){
q=current;
gets executed.
Lastly (for now), if a slot in the hash table is filled but there is no matching itemcode then the item won't get put into the hash table. Look at your code. At what point do you assign "current" to any part of the chain that starts at "list_item[pos]"? You don't. Doing "q = current" just stores one value in another variable. What you need is something like:
current->next = list_item[pos];
list_item[pos] = current;
To add it on at the beginning of the list.
I suggest you fix your file reading function before worrying about your file writing function.
P.s. an upvote and a request for more comments may get you some more help. Depending on how busy I am and whether others can also be bothered to help.