In the menu(), in case 3 and 4, modification of object information is to be done. The modification is happening but afterwards the temporary file is not being removed and the temporary file is not being renamed to the main file - "userifo.dat". Therefore whenever the program is rerun the data is inaccessibly. Is the problem not what I understand it to be??
Would love any other off-topic suggestions for improvement.
Thanks
#include<iostream>
#include<string.h>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
using namespace std;
/**************************************************************************/
struct uinfo
{
char uname[50],pass[50],secq[300],seca[300];
float mbill;
int sccode[30],scno;
void newinfo()
{
system("cls");
cout<<"Enter Username : ";
cin.ignore();
cin.getline(uname,50);
cout<<"Enter Password : ";
cin.getline(pass,50);
cout<<"Enter a security question : ";
cin.getline(secq,300);
cout<<"Enter Answer to security answer : ";
cin.getline(seca,300);
mbill=0;
scno=0;
}
}userobj;
/**************************************************************************/
struct cinfo
{
char cname[30];
int ccode;
float price;
void display()
{
cout<<"Channel name : "<<cname<<"\nPrice : "<<price<<"\nCode : "<<ccode<<endl<<endl;
}
void input()
{
system("cls");
cout<<"Enter Name Of Channel : ";
cin.ignore();
cin.getline(cname,30);
cout<<"Enter price of subscription : ";
cin>>price;
cout<<"Enter The channel code : ";
cin>>ccode;
}
}chobj;
/**************************************************************************/
void adminmenu()
{
int cho=-1;
uinfo uobj;
while(cho!=0)
{
system("cls");
cout<<"\t\t\tWelcome to the Admin Menu\n\n"
<<"Enter what you want to do : \n"
<<"1. View the usernames and the"
<<" respective Monthly bill of all Users\n"
<<"2. Remove a user\n"
<<"3. Add a channel\n"
<<"4. Modify the price of a channel\n(It will change the Monthly Bill of all"
<<" users who have subscribed the channel)\n"
<<"0. Back to normal Menu\n";
cin>>cho;
switch(cho)
{
case 1 : {
ifstream file;
file.open("userifo.dat",ios::binary);
while(file.read((char*)&uobj,sizeof(uobj)))
{
cout<<"User : "<<uobj.uname<<"\nMonthly Bill : "<<uobj.mbill<<"\n\n";
}
getch();
break;
}
case 2 : {
char tempusern[50];
int flag=0;
cout<<"Enter the username of the user whose account is to be deleted : ";
cin.ignore();
cin.getline(tempusern,50);
ifstream file;
file.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary);
while(file.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(tempusern,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
flag=1;
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
if(flag==0)
{
cout<<"No Such User....";
}
else
cout<<"User Deleted";
getch();
break;
}
case 3 : {
cinfo tempcobj;
tempcobj.input();
ofstream file;
file.open("channelinfo.dat",ios::binary|ios::app);
file.write((char*)&tempcobj,sizeof(tempcobj));
file.close();
break;
}
case 4 : {
system("cls");
ifstream file1;
file1.open("channelinfo.dat",ios::binary);
while(file1.read((char*)&chobj,sizeof(chobj)))
{
chobj.display();
}
file1.close();
cinfo tempcobj;
int tempcode,flag=0;
float cmbill,oprice;
cout<<"Enter the code of the channel that is to be updated : ";
cin>>tempcode;
ifstream file;
file.open("channelinfo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary);
while(file.read((char*)&tempcobj,sizeof(tempcobj)))
{
oprice=tempcobj.price;
if(tempcobj.ccode!=tempcode)
{
temp.write((char*)&tempcobj,sizeof(tempcobj));
}
else
{
flag=1;
cout<<"Enter the new price of the channel : ";
cin>>tempcobj.price;
temp.write((char*)&tempcobj,sizeof(tempcobj));
fstream cprice;
cprice.open("userifo.dat",ios::binary|ios::app);
ofstream temp2;
temp2.open("temp2.dat",ios::binary|ios::app);
while(cprice.read((char*)&uobj,sizeof(uobj)))
{
for(int i=0;i<uobj.scno;++i)
{
if(tempcode==uobj.sccode[i])
uobj.mbill+=(oprice-tempcobj.price);
}
temp2.write((char*)&uobj,sizeof(uobj));
}
cprice.close();
temp2.close();
remove("userifo.dat");
rename("temp2.dat","userifo.dat");
}
}
if(flag==0)
{
cout<<"No Such Refferal Code !!! Plaes Try Again....";
getch();
}
file.close();
temp.close();
remove("channelinfo.dat");
rename("temp.dat","channelinfo.dat");
break;
}
case 0 : break;
default: cout<<"You've Entered a worng choice!!! Please Try Again....";
}
}
}
/************************************************************************/
void show_subchannel()
{
int i=0;
ifstream file;
for (i;i<userobj.scno;++i) {
file.open("channelinfo.dat",ios::binary);
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(chobj.ccode==userobj.sccode[i])
chobj.display();
}
file.close();
}
getch();
}
/*************************************************************************/
void menu()
{
system("cls");
int choice=-1;
while(choice!=0)
{
system("cls");
cout<<"\t\t\t\tWelcome to the JC TV services menu\n\n\n"
<<"Enter your choice as to how you want to proceed -\n"
<<"1.View your Current Monthly Bill\n"
<<"2.View your Subscribed Channel list\n"
<<"3.Subscribe to a new Channel\n"
<<"4.Un-subscribe to a Channel\n"
<<"5.Enter into Administrator Mode\n"
<<"0.Log out and Exit\n";
cin>>choice;
switch(choice)
{
case 2:{
if(userobj.scno!=0)
{
show_subchannel();
}
else
cout<<"No Existing channels";
getch();
} break;
case 1:{
cout<<"Your Current Monthly Bill is : "<<userobj.mbill; getch();
}break;
case 3:{
ifstream file;
file.open("channelinfo.dat",ios::binary);
while(file.read((char*)&chobj,sizeof(chobj)))
{
chobj.display();
}
int tempcode;float addsum=0;
int flag=0;
for(;flag==0;)
{
cout<<"\nEnter the code of the channel you want to add : ";
cin>>tempcode;
for(int i=0;i<=userobj.scno;++i)
{
if(tempcode==userobj.sccode[i])
{
cout<<"Channel Already Subscribed.Please Enter another code";
break;
}
flag=1;
}
}
file.close();
file.open("channelinfo.dat",ios::binary);
int f=1;
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(tempcode==chobj.ccode)
{
addsum=chobj.price;
userobj.mbill+=addsum;
userobj.sccode[userobj.scno]=tempcode;
userobj.scno++;
uinfo uobj;
ifstream umfile;
umfile.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary|ios::app);
while(umfile.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(userobj.uname,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
{
temp.write((char*)&userobj,sizeof(userobj));
}
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
f=0;
}
}
if(f==1)
{
cout<<"No such refferal code...";getch();
}
}break;
case 4:{
show_subchannel();
int flag=1;
cout<<"Enter the code of the channel you want to un-subscribe to : ";
int tempcode;
cin>>tempcode;
for(int i=0;i<userobj.scno;++i)
{
if(tempcode==userobj.sccode[i])
{
flag=0;
for(int j=i;j<userobj.scno-1;++j)
userobj.sccode[j]=userobj.sccode[j+1];
userobj.scno--;
ifstream file;
file.open("channelinfo.dat",ios::binary);
float subsum;
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(tempcode==chobj.ccode){
subsum=chobj.price;
userobj.mbill-=subsum;
userobj.scno--;
}
file.close();
uinfo uobj;
ifstream umfile;
umfile.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary|ios::app);
while(umfile.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(userobj.uname,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
{
temp.write((char*)&userobj,sizeof(userobj));
}
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
}
}
if(flag==1)
{ cout<<"No such Referal Code"; getch(); }
}
}break;
case 5:{
char apass[50];
cout<<"Enter the Admin Special Password : ";
cin.ignore();
cin.getline(apass,50);
if (strcmpi(apass,"go for it")==0) {
adminmenu();
}
else
{ cout<<"Wrong Password...."; getch(); }
}break;
case 0: exit(0); break;
default:{
cout<<"Invaild Choice...";
}
}
}
}
/*************************************************************************/
void login()
{
system("cls");
char usernametemp[50],passwordtemp[50];
cout<<"Enter Username : ";
cin.ignore();
cin.getline(usernametemp,50);
cout<<"Enter Password : ";
cin.getline(passwordtemp,50);
int flag=0;
ifstream loginfile;
loginfile.open("userifo.dat",ios::binary);
while (loginfile.read((char*)&userobj,sizeof(userobj))) {
if (strcmpi(userobj.uname,usernametemp)==0) {
if (strcmpi(userobj.pass,passwordtemp)==0) {
flag=1;
menu();
}
else {
cout<<"Wrong password!!!";
cout<<"\nAnswer the following Question : \n"<<userobj.secq<<endl;
char secqa[300];
cin.getline(secqa,300);
if (strcmpi(secqa,userobj.seca)==0) {
flag=1;
menu();
}
else{
cout<<"Wrong Answer!!!! Try Again.....";
getch();
login();
}
}
}
}
if (flag==0) {
{std::cout << "No Such Username Exists!!! Try again...";
getch();
login();
}
}
loginfile.close();
}
/*************************************************************************/
void newuser()
{
int flag=1;
uinfo tempobj,tempobj2;
while(1)
{
tempobj.newinfo() ;
ifstream nufile;
nufile.open("userifo.dat",ios::binary|ios::in);
while(nufile.read((char*)&tempobj2,sizeof(tempobj2)))
{
if(strcmpi(tempobj.uname,tempobj2.uname)==0)
{
flag=0;
cout<<"Username already exists!!! Try again...";
getch();
nufile.close();
break;
}
else
flag=1;
}
if(flag==1)
{
ofstream nu1file;
nu1file.open("userifo.dat",ios::binary|ios::app);
nu1file.write((char*)&tempobj,sizeof(tempobj));
cout<<"User Created";
userobj=tempobj;
getch();
nu1file.close();
menu();
break;
}
}
}
/*************************************************************************/
int main()
{
char ch;
cout<<"\t\t\t WELCOME\n\n"
<<"Are you a new User (Y/N) : ";
cin>>ch;
if(toupper(ch)=='Y')
{
int k=1;
while(k=1)
{
newuser();
}
}
else
{
login();
}
return 0;
}
/*************************************************************************/
Related
I'm Getting a Segmentation fault here
if((cur->block)!=(cur->next)->block)
I Tried to fix Some on my own but didn't got it correct.
this Program is used to implement Control Flow Analysis it was having some more Segmentation faults which were identified and fixed ,but now. I don't have any alternative to try and edit this code.
struct Listnode
{
char data[50];
int leader,block,u_goto,c_goto;
struct Listnode *next;
char label[10],target[10];
}*temp,*cur,*first=NULL,*last=NULL,*cur1;
FILE *fpr;
void createnode(char code[50])
{
temp=(struct Listnode*) malloc(sizeof(struct Listnode));
strcpy(temp->data,code);
strcpy(temp->label,"\0");
strcpy(temp->target,"\0");
temp->leader=0;
temp->block=0;
temp->u_goto=0;
temp->c_goto=0;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
}
void main()
{
char codeline[50];
char c,dup[50],target[10];
char *substring,*token;
int i=0,j=0,block,block1;
fpr= fopen("P10-CFA.txt","r");
while((c=getc(fpr))!=EOF)
{
if(c!='\n')
{
codeline[i]=c;
i++;
}
else
{
codeline[i]='\0';
createnode(codeline);
i=0;
}
}
//create last node
codeline[i]='\0';
createnode(codeline);
fclose(fpr);
// find out leaders,conditional stmts
cur=first;
cur->leader=1;
while(cur!=NULL)
{
substring=strstr((cur->data),"if");
if(substring==NULL)
{
if((strstr((cur->data),"goto"))!=NULL)
{
cur->u_goto=1;
(cur->next)->leader=1;
}
}
else
{
cur->c_goto=1;
(cur->next)->leader=1;
}
substring=strstr((cur->data),":");
if(substring!=NULL)
{
cur->leader=1;
}
substring=strstr((cur->data),"call");
if(substring!=NULL)
{
cur->leader=1;
}
if(strstr(cur->data,"return")!=NULL)
{
cur->leader=1;
(cur->next)->leader=1;
}
cur=cur->next;
//to find labels and targets
cur=first;
while(cur!=NULL)
{
if((cur->u_goto==1)||(cur->c_goto==1))
{
substring=strstr(cur->data,":");
if(substring!=NULL)
{
token=strstr(substring,"L" );
if(token!=NULL)
strcpy(cur->target,token);
else
{
substring=strstr(cur->data,"L");
if(substring!=NULL)
strcpy(cur->target,substring);
}
}
if(strstr(cur->data,":")!=NULL)
{
strcpy(dup,cur->data);
token=strtok(dup,":");
//printf("\ntoken:%s",token);
if(token!=NULL)
strcpy(cur->label,token);
}
cur=cur->next;
//to identify blocks
cur=first;
while(cur!= NULL)
{
cur=cur->next;
if((cur->leader)==1)
{
j++;
cur->block=j;
}
else
cur->block=j;
}
printf("\n\n.....Basic Blocks \n");
cur=first;
j=0;
printf("\nBlock %d:",j);
while(cur!=NULL)
{
if ((cur->block)==j)
{
printf("%s",cur->data);
printf("\n\t");
cur=cur->next;
}
else
j++;
printf("\nBlock %d:",j);
}
}
//to output the control flow from each block
printf ("\t\t.......Control Flow.........\n\n");
cur=first;
i=0;
while(cur!=NULL)
{
if((cur->block)!=(cur->next)->block)
{
block=cur->block;
if(cur->u_goto==1)
{
strcpy(target,cur->target);
cur1=first;
while(cur1!=NULL)
{
if(strcmp(cur1->label,target)==0)
{
block1=cur1->block;
printf("\t\tBlock%d---->Block%dln",block,block1);
}
cur1=cur1->next;
}
}
else if(cur->c_goto==1)
{
strcpy(target,cur->target);
cur1=first;
while(cur1!=NULL)
{
if(strcmp(cur1->label,target)==0)
{
block1=cur1->block;
printf("lt\1Block%d---TRUE--->Block%d---FALSE--->Block%d\n",block,block1,(block+1));
}
cur1=cur1->next;
}
}
else if(strstr(cur->data,"return")==NULL)
{
printf("\t\tBlock%d--->Block%d\n",block,(block+1));
}
else
printf("lt\tBlock%d--->NULL\n",block);
}
cur=cur->next;
}
cur=last;
block= cur->block;
printf("\t\tBlock%d--->NULL",block);
}
}
}
#include<iostream>
#include<algorithm>
#define INT unsigned __int128
using namespace std;
INT base[25][25],lit[25],n,error=0;
void expbase(INT p,INT q)
{
for(INT i=1;i<=n;i++)
{
base[p][i]=1;
base[i][q]=1;
}
INT cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp--;
cq++;
}
cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp++;
cq--;
}
cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp--;
cq--;
}
cp=p,cq=q;
while(cp>=1&&cp<=n&&cq>=1&&cq<=n)
{
base[cp][cq]=1;
cp++;
cq++;
}
base[p][q]=2;
return;
}
void make(INT ed)
{
INT d[25][25]={0,},ch=0,mult=1;
for(INT i=1;i<=n;i++)
{
INT cop[25][25]={0,},co=0;
if(lit[i])continue;
for(INT p=1;p<=n;p++)
{
for(INT q=1;q<=n;q++)
{
cop[p][q]=d[p][q];
d[p][q]=0;
}
}
if(i==ed)
{
/*Insert Queen*/
for(INT j=1;j<=n;j++)
{
if(cop[i][j]!=(1<<mult)-1&&!base[i][j]){expbase(i,j);return;}
else if(j==n){error=1;return;}
}
}
else
{
/*Check the possibility*/
for(INT j=1;j<=n;j++)
{
if(!base[i][j])
{
INT ch[25][25]={0,};
for(INT p=1;p<=n;p++)
for(INT q=1;q<=n;q++)
d[p][q]=(d[p][q]<<mult);
for(INT p=1;p<=n;p++)if(!ch[i][p]){d[i][p]+=(1<<mult)-1;ch[i][p]=1;}
for(INT p=1;p<=n;p++)if(!ch[p][j]){d[p][j]+=(1<<mult)-1;ch[p][j]=1;}
INT ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci--;cj++;
}
ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci++;cj++;
}
ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci++;cj--;
}
ci=i,cj=j;
while(ci>=1&&ci<=n&&cj>=1&&cj<=n)
{
if(!ch[ci][cj]){d[ci][cj]+=(1<<mult)-1;ch[ci][cj]=1;}
ci--;cj--;
}
for(INT p=1;p<=n;p++)
for(INT q=1;q<=n;q++)
if(!ch[p][q])d[p][q]+=(cop[p][q]|cop[i][j]);
co++;
}
}
mult*=co;
}
}
return;
}
int main()
{
scanf("%lld",&n);
for(INT i=1;i<=n;i++)
{
for(INT j=1;j<=n;j++)
{
scanf("%lld",&base[i][j]);
if(base[i][j]==2)lit[i]=1;
}
}
cout<<endl;
for(INT i=n;i>=1;i--)
{
if(!lit[i])make(i);
if(error){cout<<"does not exist"<<endl;return 0;}
}
for(INT i=1;i<=n;i++)
for(INT j=1;j<=n;j++)
if(base[i][j]==0)base[i][j]=2;
cout<<endl;
for(INT i=1;i<=n;i++)
{
for(INT j=1;j<=n;j++)
printf("%llx ", (unsigned long long)(base[i][j] & 0xFFFFFFFFFFFFFFFF));
cout<<endl;
}
return 0;
}
The above code is written to solve the n-queens problem, which is the problem presented to solve the P versus NP problem.
Currently, the above code only works if the multiplication of the number of zeros present in each column is 128 or less.
I wonder if I can solve the n-queens problem by making several modifications to this algorithm.
The below code in c++ is to find the distance between any two nodes in a graph but there is some logical error where I tried to find but can't. Everything seems to be perfect but I can say that there is something wrong with a break inside if condition inside for loop which is inside while(s.!empty()).
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int find_distance(int link[100][100],int start,int dest,int n,int e)
{
start=start-1;
dest=dest-1;
if(start==dest)return 0;
int visited[100];
int distance;
for(int i=0;i<n;i++)
{
visited[i]=0;
}
stack<int>s;
int k;s.push(start);
visited[start]=1;
bool found=false;
int count =0;
while(!s.empty())
{
k=s.top();
int i;
int check_point=1;
for(i=0;i<n;i++)
{
if(link[k][i]==1 && visited[i]!=1)
{
s.push(i);
count ++;
check_point=0;
if(i==dest)
{
cout<<"found";
found=true;
}
break;
}
}
if(check_point==1)
{
s.pop();
count--;
}
if(found)break;
}
return count;
}
int main()
{
int n,e,a,b;
int link[100][100];
cout<<"enter the number of vertices and edges";
// cin>>n>>e;
n=5;e=4;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
link[i][j]=0;
else
link[i][j]=INT_MAX;
cout<<link[i][j]<<" ";
}
cout<<endl;
}
int input[4][2]={{1,2},{1,3},{2,4},{2,5}};
for(int i=0;i<e;i++)
{
a=input[i][0];
b=input[i][1];
link[a-1][b-1]=1;
link[b-1][a-1]=1;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<link[i][j]<<" ";
}
cout<<endl;
}
while(true) {
cout<<endl<<"enter the starting point .. ";
int start;
cin>>start;
int dest;
cout<<endl<<"enter the destination point .. ";
cin>>dest;
int distance = find_distance(link,start,dest,n,e);
cout<<endl<<"distance is "<<distance;
}
return 0;
}
Can anyone prove/derive the time complexity of my nqueens solution approach?
I am going through each and every position on the grid and if it is possible to place a queen there, then, I am calculating solution by first placing the queen and then unplacing the queen, else I move on.
Code:
bool notinrow(int row,int col,vector<string> tra)
{
for(int i=0;i<tra.size();i++)
{
if(tra[row][i]=='Q' & i!=col)
return false;
}
return true;
}
bool notincol(int row,int col,vector<string> tra)
{
for(int j=0;j<tra.size();j++)
{
if(tra[j][col]=='Q' & j!=row)
return false;
}
return true;
}
bool notindiag1(int r,int c, vector<string> tra)
{
int i=r-1;
int j=c-1;
while(i>=0 & j>=0)
{
if(tra[i][j]=='Q')
return false;
i--;j--;
}
return true;
}
bool notindiag2(int r, int c,vector<string> tra)
{ int i=r-1;
int j=c+1;
while(i>=0 & j<totqueens)
{
if(tra[i][j]=='Q')
return false;
i--;j++;
}
return true;
}
void nqueens(int number,vector<string> tra,int currqueens)
{
if(currqueens==totqueens)
{
bhej.push_back(tra);
return ;
}
if(number==totiter)
return;
int x=number/totqueens;
int y=number%totqueens;
if(ispossible(x,y,tra))
{
tra[x][y]='Q';
nqueens(number+1,tra,currqueens+1);
tra[x][y]='.';
nqueens(number+1,tra,currqueens) ;
}
else
nqueens(number+1,tra,currqueens);
}```
I am having problem with this code.Can someone help me to fix it ?
Problem is in insert function especially with malloc(allocating).I don't understand why I cannot malloc.It is not printing the debug text.
#include<stdlib.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
struct ListRecord
{
struct Node *head;
struct Node *tail;
int length;
};
typedef struct ListRecord *DoubleList;
DoubleList createList()
{
return NULL;
}
DoubleList MakeEmptyList(DoubleList l)
{
l = (struct ListRecord*)malloc(sizeof(struct ListRecord));
if(l==NULL)
{
printf("Memory allocation failed!");
}
else
{
l->head->next=NULL;
l->head->prev=NULL;
l->tail=l->head;
l->length=0;
}
}
DoubleList InsertListAtPosition(DoubleList l,int pos,int val)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
printf("debug");
if(newNode==NULL)
{
printf("debug");
newNode->data=val;
newNode->next=NULL;
newNode->prev=NULL;
if(pos > l->length+1)
{
pos=l->length+1;
}
else if(pos==l->length+1)
{
struct Node *iterator=l->head;
while(iterator->next != NULL)
{
iterator=iterator->next;
}
iterator->next=newNode;
newNode->prev=iterator;
newNode->next=NULL;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=0;i<pos;i++)
iterator=iterator->next;
newNode->next=iterator->next;
newNode->prev=iterator;
iterator->next->prev=newNode;
iterator->next=newNode;
}
l->length++;
}
else
{
printf("Memory allocation failed!");
}
}
int DeleteListAtPosition(DoubleList l,int pos)
{
int value;
if(l==NULL)
{
printf("List is empty");
return 0;
}
else
{
if(pos==1)
{
struct Node* iterator=l->head;
value=iterator->data;
free(iterator);
return value;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=1;i<pos;i++)
iterator=iterator->next;
if(iterator->next==NULL)
{
value=iterator->data;
free(iterator);
return value;
}
else
{
iterator->prev->next=iterator->next;
iterator->next->prev=iterator->prev;
free(iterator);
}
}
}
}
void printList(DoubleList l) {
struct Node* temp = l->head;
printf("Forward: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
DoubleList myList;
int exit,pos,value;
char command;
myList=createList();
exit=FALSE;
while(!exit)
{
fflush(stdin);
printf("\nMenu:\n m)akeEmpty\n i)nsert\n e)xit\n");
scanf("%c", &command);
fflush(stdin);
switch(command)
{
case 'm':
myList = MakeEmptyList(myList);
break;
case 'i':
printf("Enter position to be added: ");
scanf("%d",&pos);
printf("Enter value to be added: ");
scanf("%d",&value);
InsertListAtPosition(myList,pos,value);
//printList(myList);
break;
case 'e':
exit = TRUE;
break;
default:
printf("command not recognized\n");
break;
}
}
printf("\n\n");
system("PAUSE");
return 0;
}
Here is the working code,
#include<stdlib.h>
#include<stdio.h>
#include <bits/stdc++.h>
#define TRUE 1
#define FALSE 0
using namespace std;
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
struct ListRecord
{
struct Node *head;
struct Node *tail;
int length;
};
typedef struct ListRecord *DoubleList;
DoubleList createList()
{
return NULL;
}
DoubleList MakeEmptyList()
{
DoubleList l;
l = (struct ListRecord*)malloc(sizeof(struct ListRecord));
if(l==NULL)
{
printf("Memory allocation failed!");
}
else
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = NULL;
newNode->prev = NULL;
l->head = newNode;
l->tail=l->head;
l->length=0;
}
return l;
}
DoubleList InsertListAtPosition(DoubleList l,int pos,int val)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if(newNode != NULL)
{
newNode->data=val;
newNode->next=NULL;
newNode->prev=NULL;
if(pos > l->length)
{
pos=l->length+1;
}
else if(pos==l->length)
{
struct Node *iterator=l->head;
while(iterator->next != NULL)
{
iterator=iterator->next;
}
iterator->next=newNode;
newNode->prev=iterator;
newNode->next=NULL;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=0;i<pos;i++)
iterator=iterator->next;
newNode->next=iterator->next;
newNode->prev=iterator;
iterator->next->prev=newNode;
iterator->next=newNode;
}
l->length++;
}
else
{
printf("Memory allocation failed!");
}
}
int DeleteListAtPosition(DoubleList l,int pos)
{
int value;
if(l==NULL)
{
printf("List is empty");
return 0;
}
else
{
if(pos==1)
{
struct Node* iterator=l->head;
value=iterator->data;
free(iterator);
return value;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=1;i<pos;i++)
iterator=iterator->next;
if(iterator->next==NULL)
{
value=iterator->data;
free(iterator);
return value;
}
else
{
iterator->prev->next=iterator->next;
iterator->next->prev=iterator->prev;
free(iterator);
}
}
}
}
void printList(DoubleList l) {
struct Node* temp = l->head;
printf("Forward: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
DoubleList myList;
int exit,pos,value;
char command;
exit=FALSE;
while(!exit)
{
fflush(stdin);
printf("\nMenu:\n m)akeEmpty\n i)nsert\n e)xit\n");
scanf("%c", &command);
fflush(stdin);
switch(command)
{
case 'm':
myList = MakeEmptyList();
break;
case 'i':
printf("Enter position to be added: ");
scanf("%d",&pos);
printf("Enter value to be added: ");
scanf("%d",&value);
if(myList == NULL) {
myList = MakeEmptyList();
}
InsertListAtPosition(myList,pos,value);
//printList(myList);
break;
case 'e':
exit = TRUE;
break;
default:
printf("command not recognized\n");
break;
}
}
printf("\n\n");
system("PAUSE");
return 0;
}
So you were making the following mistakes,
Your MakeEmptyList method is returning nothing, it should return l.
Inside you insert you were checking with length + 1, instead of only length, so it was causing pointer related errors.