Hi guys!I'm in great need and embarassment.I'm new to the C++ program and to be frank I'm having a hard time understanding it. Now our topic is about linked list and my professor gave us this machine problems:

1. Write a program that implements a linked list using an array.Your program should be a menu diven and will prompt the user to insert data,delete data,display the contents of the list,or search the list for a given piece of data. To insert new data, you must delete an element from the available list and then insert into the data list.Conversely,when you delete a node from the data list, you must insert it into the available list.

2. Write a function that merges two ordered linked lists into one list. When two lists are merged, the data in the resulting list are also ordered. The two original lists should be left unchanged; that is, the merged list shoul be a new list.

Moreover, my prof gave me this sample program that will help me solve the problem no 1.

Please help me.I need this by tomorrow...

Attachments
#include<iostream.h>


class node
{
public:
	int info;
	node *link;
};

//declare global objects
node *start=NULL;

//function prototypes
void insert(int,int);
void add(int);
void display(void);
void del(int);
void free();

void main(void)
{
	int ch,pos,num;

while(ch!=5)
{
	cout<<"1> Add";
	cout<<"\n2> Insert";
	cout<<"\n3> Delete";
	cout<<"\n4> Display";
	cout<<"\n5> Quit\n";

	cin>>ch;

	switch(ch)
	{
		case 1:
		cout<<"Enter element:";
		cin>>num;

		add(num);
		break;

		case 2:
		cout<<"Enter pos:";
		cin>>pos;
		cout<<"Enter element:";
		cin>>num;

		insert(pos,num);
		break;

		case 3:
		cout<<"Enter element to be deleted:";
		cin>>num;
		del(num);
		break;

		case 4:
		display();
		break;
	}
}

//free-up the allocated memory
free();
}


//--FUNCTION--
//This functon  takes two arguments
//'loc' is the number of elements after
//which the new element having 
//value 'inf' has to be inserted

void insert(int loc,int inf)
{
	node *temp;
	node *temp_new;
	int i;

	//if invalid,return if(loc<=0) return;

	temp=start;

	//skip to the desire location where th node is to be added

	for(i=1;i<loc;i++)
	{
		temp=temp->link;
		if(temp==NULL)
		{
			cout<<"NOT POSSIBLE";
			return;
		}
	}
	
	//allocate new node
	temp_new=new node;
	temp_new->info=inf;

	//make it point to the respective node
	temp_new->link=temp->link;

	//make the prev.node where the new node is added to
	//point at the new node
	temp->link=temp_new;
}

//--FUNCTION--
//display all tha data in the linked list

void display(void)
{
	node *temp;
	temp=start;

	//traverse or process
	//through each element
	//and keep printing the information

	cout<<"\nelements are...\n";
	while(temp!=NULL)
	{
		cout<<temp->info<<endl;
		temp=temp->link;
	}
}

//--FUNCTION--
//adds node to the end of the linked list

void add(int inf)
{
	node *temp1;
	node *temp2;

	//if the element to be added is the first element
	if(start==NULL)
	{
	//allocate a new node
		temp1=new node;
		temp1->info=inf;
		temp1->link=NULL;

	//make start point at it
		start=temp1;
	}
	//if not
	else
	{
		temp1=start;

	//find out the last element
		while(temp1->link!=NULL)
		temp1=temp1->link;

	//allocate new node
		temp2=new node;
		temp2->info=inf;
		temp2->link=NULL;

	//make the last element of the list to point
	//at the newly created node
		temp1->link=temp2;
	}
}

//--FUNCTION--
//takes the argument of the 'info' part of the node
//to be deleted

void del(int inf)
{
	node *temp,*old;

	temp=start;

	//while list is not empty
	while(temp!=NULL)
	{
	//if match is found
		if(temp->info==inf)
		{
		//if it is the first node
		if(temp==start)
			start=temp->link;

		else
			old->link=temp->link;

		delete temp;
		return;
		}
		else
		{
		//travese through each node
			old=temp;
			temp=temp->link;
		}
	}
}

//--FUNCTION--
//free up the allocated memory
//used by the nodes of the linked list

void free()
{
	//below is a bit confysing part
	//here are all nodes that we have allocated
	//are being freed up

	node temp;
	while(start!=NULL)
	{
		//store the next node
		//to the one being deleted

		temp=*start;

		//delete the node
		delete start;

		//retrieve the next node to be deleted
		start=temp.link;
	}
}

>my prof gave me this sample program
If you haven't modified it, then I would suggest you drop that professor and find another that can actually teach you C++.

>Please help me.I need this by tomorrow...
Please show us your attempt. If you need this by tomorrow then that means you've had plenty of time to work on it, so you should have plenty of code and experience to describe what you tried and how it didn't work.

If you haven't modified it, then I would suggest you drop that professor and find another that can actually teach you


What do you mean by this?Is my professor not good enough? Well..I'm really clueless about linked list.She gave this problem just hours ago and I need to submit it by tomorrow..

>What do you mean by this?
I mean that your professor is either incompetent or doesn't care enough to learn C++ properly and thus isn't qualified to teach it.

>Is my professor not good enough?
Well, the code you attached is full of poor programming practices and broken code. If that's an example of your professor's programming ability, I wouldn't expect to get much out of the course.

>Well..I'm really clueless about linked list.
This is still incomplete until I find the time to finish it, but it still covers the basics.

>She gave this problem just hours ago and I need to submit it by tomorrow..
Sorry, but I don't believe you. If you're as new to linked lists as you claim, there's no way you would be expected to completely solve both of those problems in one evening (without cheating, of course).

This article has been dead for over six months. Start a new discussion instead.