8 Replies - 1435 Views - Last Post: 10 October 2012 - 11:49 AM Rate Topic: -----

#1 rock9212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 12

want to add number either at the end or mid of user given list

Posted 10 October 2012 - 06:52 AM

this is my code which adds number at the start of given user list
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{

int info;
struct node*link;
}*start;
void add(int data)
{
struct node*temp;
temp=(node*)malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
}
void create_list(int data)
{
struct node*q, *temp;
temp=(node*) malloc(sizeof(struct node));
temp-> info=data;
temp-> link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q-> link!=NULL)
q=q-> link;
q-> link=temp;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
cout<<" we got an empty linked list";
return;
}
q=start;
cout<<endl;
cout<<"List is  ";
while(q!=NULL)
{
	cout<<q->info;
q=q->link;
}
}

int main()
{
int a;
cout<<" Please enter value SIR"<<endl;
start=NULL;
for(int i=0;i<10;i++)
{
cin>>a;
//a=10;
create_list(a);
}
display();
add(8);
display();
system("pause");
}

This post has been edited by jimblumberg: 10 October 2012 - 06:56 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: want to add number either at the end or mid of user given list

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,897
  • Joined: 25-December 09

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 07:01 AM

First please use code tags when posting code.

:code:

Since this seems to be C++ don't use malloc.

Next find an indentation style you like and use it consistently.

Lastly, what is your actual question?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 rock9212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 12

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 08:21 AM

]
Yes u r right this is c++ and my program runs on Dev C++.we are asked to use malloc for memory allocation...if we run this program it puts the number in add(8) at the start of numbers which we entered...MODIFICATION which i want is to place that number either at the end or at the mid of user entered number...the program is given below

[
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{

int info;
struct node*link;
}*start;
void add(int data)
{
struct node*temp;
temp=(node*)malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
}
void create_list(int data)
{
struct node*q, *temp;
temp=(node*) malloc(sizeof(struct node));
temp-> info=data;
temp-> link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q-> link!=NULL)
q=q-> link;
q-> link=temp;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
cout<<" we got an empty linked list";
return;
}
q=start;
cout<<endl;
cout<<"List is  ";
while(q!=NULL)
{
	cout<<q->info;
q=q->link;
}
}

int main()
{
int a;
cout<<" Please enter value SIR"<<endl;
start=NULL;
for(int i=0;i<10;i++)
{
cin>>a;
//a=10;
create_list(a);
}
display();
add(8);
display();
system("pause");
}

]

MOD EDIT: PLEASE USE CODE TAGS WHEN POSTING CODE!!

:code:

.

This post has been edited by jimblumberg: 10 October 2012 - 08:27 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,897
  • Joined: 25-December 09

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 08:30 AM

First please use code tags when posting code!!!

If you are being taught to use malloc in a C++ program, you are being cheated. You should not be using malloc in a C++ program, use new/delete. The malloc program doesn't properly call C++ constructors and destructors which can cause many problems.

Next show what you have tried to accomplish your objectives.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 08:35 AM

I'm confused. The OP says that the current code adds numbers to the start of the list, but lines 30-33 append to the end of the list.

Anyway, what is the decision process for determining whether a number should go to the middle or the end of the list? Is it a random process? Does it depend on the value of the number? Does it depend on the phase of the moon?
Was This Post Helpful? 0
  • +
  • -

#6 rock9212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 12

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 09:15 AM

This is a linked list task...my program declares the linked list and display the first 10 nodes and add the number at 1st position...now the change in this task is to add the nodes at the start and at the end after the creation of linked list...we are suggested that this can be done by traversing and using cases...but i am not clear how to actually make it ...the modification is just 4 lines but i am not clear how to do it...hope u understand my question
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 09:33 AM

I guess I don't understand your question or what you are trying to do. Your code already has all it needs except the need to just add 2 more function calls.

Your lines 59-64 add to the end of the list using create_list(). Line 65 prints out the contents of the list. Line 66 adds to the beginning of the list. Line 67 displays that new list.

So what is wrong with just calling create_list() with another number to add to end of the list, and displaying that new list? That is a 1 line modification to do add a new item, and another line to display.
Was This Post Helpful? 0
  • +
  • -

#8 rock9212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 12

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 10:57 AM

ok thnx for help i will create another function...
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: want to add number either at the end or mid of user given list

Posted 10 October 2012 - 11:49 AM

Okay, if you want to.

This is something that you'll learn later as you go through you programming career: Minimize duplicated code. Unless you change create_list() to call your new function (as well as call your old add() function), now you'll have two places in your code that has very similar logic that needs to be maintained.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1