Answer to Question #259418 in C++ for anu

Question #259418

Find the three strings

b)from circular header lists from the one way lists using char[20]


1
Expert's answer
2021-11-01T01:59:08-0400
#include<bits/stdc++.h>
using namespace std;


struct Node
{
	int d;
	struct Node *nt;
};


struct Node *addToEmpty(struct Node *lt, int d)
{
	if (lt != NULL)
	return lt;
	struct Node *temp =
		(struct Node*)malloc(sizeof(struct Node));


	temp -> d = d;
	lt = temp;


	lt -> nt = lt;


	return lt;
}


struct Node *addBegin(struct Node *lt, int d)
{
	if (lt == NULL)
		return addToEmpty(lt, d);


	struct Node *temp =
			(struct Node *)malloc(sizeof(struct Node));


	temp -> d = d;
	temp -> nt = lt -> nt;
	lt -> nt = temp;


	return lt;
}


struct Node *addEnd(struct Node *lt, int d)
{
	if (lt == NULL)
		return addToEmpty(lt, d);
	
	struct Node *temp =
		(struct Node *)malloc(sizeof(struct Node));


	temp -> d = d;
	temp -> nt = lt -> nt;
	lt -> nt = temp;
	lt = temp;


	return lt;
}


struct Node *addAfter(struct Node *lt, int d, int item)
{
	if (lt == NULL)
		return NULL;


	struct Node *temp, *p;
	p = lt -> nt;
	do
	{
		if (p ->d == item)
		{
			temp = (struct Node *)malloc(sizeof(struct Node));
			temp -> d = d;
			temp -> nt = p -> nt;
			p -> nt = temp;


			if (p == lt)
				lt = temp;
			return lt;
		}
		p = p -> nt;
	} while(p != lt -> nt);


	cout << item << " not present in the list." << endl;
	return lt;


}


void traverse(struct Node *lt)
{
	struct Node *p;


	if (lt == NULL)
	{
		cout << "Empty." << endl;
		return;
	}


	p = lt -> nt;
	do
	{
		cout << p -> d << " ";
		p = p -> nt;


	}
	while(p != lt->nt);


}
int main()
{
	struct Node *lt = NULL;


	lt = addToEmpty(lt, 1);
	lt = addBegin(lt, 2);
	lt = addBegin(lt, 3);
	lt = addEnd(lt, 4);
	lt = addEnd(lt, 5);
	lt = addAfter(lt, 8, 7);


	traverse(lt);


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS