Find the three strings
b)from circular header lists from the one way lists using char[20]
#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;
}
Comments
Leave a comment