Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,172 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,921 people online right now. Registration is fast and FREE... Join Now!




please I need the answer of solving link list problem

 
Reply to this topicStart new topic

please I need the answer of solving link list problem, link list

mohamed moghnia
6 Dec, 2007 - 02:14 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 4


My Contributions
hello, please I need some one to solve my assignment in link list
and I would appreciate your feedback

Write a program that maintains and operates on lists of integers. The user of the program may
print a list, start a new list, add or delete integers from a list, etc. The program reads instructions
from a text file called hw04.txt. Each instruction is specified on a separate text line. After an
instruction is performed, the program should take another instruction until instructed to quit. Here’s a
complete list of possible instructions:
w x → switch to list # x, thus list # x becomes the current list. The program maintains 9 lists,
numbered 1 to 9. Initially, list # 1 is the current list.
p → print the current list.
n → clears the current list to empty.
a x → add integer x to the current list. if x already in the list, ignore, but prints a warning
message.
d y → delete integer y from the current list. print a warning message if y is not in the list.
c → print the number of elements in the current list.
s → print the sum of the elements in the current list.
g z → sets the current list to a new list made of the first z fibonacci numbers.
q → quits the program.
The List must be maintained in an ascending order.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Please I Need The Answer Of Solving Link List Problem
6 Dec, 2007 - 03:02 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
We can certainly HELP you with your question if you would just provide the code you already have. Don't forget to put it in code tags and then we can see where you are going wrong and help you out. Help us help you and the world will be a better place.

I am looking forward to seeing your code. smile.gif
User is offlineProfile CardPM
+Quote Post

mohamed moghnia
RE: Please I Need The Answer Of Solving Link List Problem
7 Dec, 2007 - 06:44 AM
Post #3

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 4


My Contributions
I need some one to find out the errors please
CODE

#include<stdio.h>
#include<string.h>
#define N 8
struct node {
int data;
struct node* next;
};
struct node* switchpointer;
void wx(int x){   /*Switch List*/
struct node* list[N];
switch(x){
case 1:switchpointer=list[x-1];
break;
case 2:switchpointer=list[x-1];
break;
case 3:switchpointer=list[x-1];
break;
case 4:switchpointer=list[x-1];
break;
case 5:switchpointer=list[x-1];
break;
case 6:switchpointer=list[x-1];
break;
case 7:switchpointer=list[x-1];
break;
case 8:switchpointer=list[x-1];
break;
case 9:switchpointer=list[x-1];
break;
default:printf("Sorry the list is not available\n");
break;
}
}
void p(struct node* switchpointer){  /*Printing the current list using the head switchpointer*/
struct node* current=switchpointer;
while(current!=NULL){
printf("%d\t",current->data);
current=current->next;
}
}
void n(struct node** switchpointer){ /*Delete the current list*/
struct node* current=*switchpointer;
struct node* temp;
while(current!=NULL){
  temp=current->next;
  deallocate(current);
  current=temp;
}
*switchpointer=NULL;
}
struct node* allocate(struct node** newnode) {  //Allocate Newnode//
int *AC;
(*AC)++;
*newnode =(struct node*) malloc(sizeof(struct node));
return newnode;
}
void deallocate(struct node* p) {  //Deallocate the first node in the current list//
int *DC;
(*DC)++;
free(p);
}
void ax(struct node** newnode,int data){ //Add x to the current list//
allocate(current)=*newnode;
while(current!=NULL){
  if(newnode->data!=current->next->data){
  current=current->next;
}
  else if(newnode->data==current->data){
  printf("Number already exist..\n");
  break;
}
}
newnode->data=data;
newnode->next=NULL;
}
}
void dy(struct node** head,int data){ //Delete int y from the list//
allocate(current)=*head;
allocate(pre);
pre=current;
while(current!=NULL){
  if(current->data==data && current->next==NULL){
  deallocate(current);
  *head=NULL;
  }
  else if(current->data!=data){
   pre=current;
   current=current->next;
  }
  else if(current->data==data && current->next!=NULL){
   pre->next=current->next->next;
   deallocate(current);
  }
}
if(current==NULL){
printf("List is empty\n");
}
void c(struct node* switchpointer){  //Print the whole current list//
allocate(current);
current=switchpointer;
int i=1;
if(current==NULL)
  printf("List is Empty\n");
  while(current->next!=NULL){
   printf("Node#%d contains:%d\n",i,current->data);
   i++;
   current=current->next;
  }
}
void s(struct node* switchpointer){  //Print the sum of the current list//
int sum=0;
allocate(current);
current=switchpointer;
if(current==NULL){
printf("Empty list Sum=0\n");
    }
while(current!=NULL){
  sum++;
  current=current->next;
}
printf("Sum = %d\n"sum);
}
void gz(struct node** switchpointer,int given){//Overwrite the current nodelist with shwayat fabonnaci//
int i;
n(switchpointer);
for(i=0;i=<given;i++){
ax(switchpointer,fib(i));
}
}
int fib(int num){      //Fabonacci//
if(num==0 || num==1)
  return num;
return (fib(num-1)+fib(num-2));
}
}
main(){
struct node* list[N];
switchpointer=list[0];  /*Default switchpointer points to list1*/
int *AC=0;  // how many times allocate() was called pointer.//
int *DC=0;  //how many times deallocate() was called pointer.//
FILE *hw04;
int numb1;
char str[10];
hw04=fopen("C:/hw04.txt","r");
while(!feof(hw04)){
fscanf(hw04,"%s",str);
if(strcmp(str,"w")==0){
    fscanf(hw04,"%d",&numb1);
    wx(numb1);
   }
   else if(strcmp(str,"p")==0){
    p(switchpointer);
   }
   else if(strcmp(str,"n")==0){
    n(&switchpointer);
   }
   else if(strcmp(str,"ax")==0){
    fscanf(hw04,"%d",numb1);
    ax(&switchpointer,numb1);
   }
   else if(strcmp(str,"dy")==0){
    fscanf(hw04,"%d",numb1);
    dy(&switchpointer,numb1);
   }
   else if(strcmp(str,"c")==0){
    c(&switchpointer);
   }
   else if(strcmp(str,"s")==0){
    s(switchpointer);
   }
   else if(strcmp(str,"g")==0){
    fscanf(hw04,"%d",&numb1);
    gz(&switchpointer,numb1);
   }
   else if(strcmp(str,"c")==0){
    c(&switchpointer);
   }
   else if(strcmp(str,"q")==0){
   break;
   }
   printf("#Allocate=%d Deallocate=%d\n",*AC,*DC);
  }
  fclose(hw04);
}

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Please I Need The Answer Of Solving Link List Problem
7 Dec, 2007 - 07:32 AM
Post #4

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,931



Thanked: 47 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Please post your code like the rules say: code.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Please I Need The Answer Of Solving Link List Problem
7 Dec, 2007 - 08:42 AM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
While some of us pride ourselves on being human equivalents to computer compilers (myself included) your compiler should be telling you any syntax errors. Can you display them for us? If there are no syntax errors, can you explain what it is that is going wrong with your code? What is it suppose to do and do you have a copy of the hw04.txt you can show us since that is your input file?

The more you can tell us about what is wrong and what you need help with, the better we can help! Thanks a bunch! smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month