Member Avatar for HASHMI007

Assume the following specification of a node of linked structure and the class

// LinkedStructure And the class.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

using namespace std;

struct Node{

	int info;
	struct Node *next;
};
typedef struct Node * NODEPTR ;
NODEPTR listptr;
class linkStr{
private :
	NODEPTR ptr;
	linkStr(){
		ptr=0;
	}
	~linkStr();
//creat a likend structure of length pointed to by ptr 
//the value of info part are input from the keyboard
	void makeStr(int len);
//Display all the element of linked structure pointed to by ptr on the secreen 
	void DisplayStr();
//remove the first elmet of liked structure pointed to by ptr
	void removeFirst();
//remove the last elmet of liked structure pointed to by ptr
	void removeLast();
//Remove the first elemetn of liked structure with an info feild equal to k.
//if no info element or the lisst is empty ,Do nothing 
	void remove(int k);
	bool IsEmpty(){
		return (ptr==0);
	}

};
linkStr::~linkStr(){
		NODEPTR p,q;
	if(IsEmpty())
		exit(1);
	for (p=ptr ,q=p->next ; p!=0 ; p=q, q=p->next)
		delete p;
	}
void linkStr::makeStr(int len){

	NODEPTR p,q;
	
	
	for (int i=0;i<len;i++){
		int value;
		cin>>value;
		q = new Node;
	q->info=value;
	q->next=p->next;
	p->next=q;
    }
}
void linkStr::removeFirst(){

	NODEPTR p ,q;
	p=listptr;
	if (p==0){

		cout<<"Error";
	}
	else
		q->next=p->next;
	delete p;
}
void linkStr::removeLast(){
	NODEPTR p,q;

	

}

}

/*Write a driver program for the implementation of the class LinkStr;*/
Any one plxx help me to write the Function

void linkStr::makeStr(int len);

help me please

what seems to be the problem senor

Member Avatar for HASHMI007

Dear Problem

void linkStr::makeStr(int len);

in this Function when first time loop Run it is ok when time loop Execute it Create a Error of Processor Fault at this point Stuck

Does makeStr() append the existing list or it just creates a new list over the existing list? What you are doing now is none of the above but create nodes with links... Then the created list is discarded and becomes memory leak after the function ends.

Member Avatar for HASHMI007
void linkStr::makeStr(int len){
 
	NODEPTR p,q;
 
 
	for (int i=0;i<len;i++){
		int value;
		cin>>value;
		q = new Node;
	q->info=value;
	q->next=p->next;
	p->next=q;
    }

Not working why

At line 11, you assign q->next equal to p->next, but p isn't defined. I suspect that what this function is -supposed- to do is append the len new nodes to the end of the list, so you need to initialize p by stepping through the list, starting at ptr, until p->next is NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.