Data Structure

#include

using namespace std;

class linklist
{
private:

struct node
{
int data;
node *link;
}*p;

public:

linklist();
void append( int num );
void add_as_first( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~linklist();
};

linklist::linklist()
{
p=NULL;
}

void linklist::append(int num)
{
node *q,*t;

if( p == NULL )
{
p = new node;
p->data = num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;

t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}

void linklist::add_as_first(int num)
{
node *q;

q = new node;
q->data = num;
q->link = p;
p = q;
}

void linklist::addafter( int c, int num)
{
node *q,*t;
int i;
for(i=0,q=p;i
{
q = q->link;
if( q == NULL )
{
cout<<"\nThere are less than "<<<" elements.";
return;
}
}

t = new node;
t->data = num;
t->link = q->link;
q->link = t;
}

void linklist::del( int num )
{
node *q,*r;
q = p;
if( q->data == num )
{
p = q->link;
delete q;
return;
}

r = q;
while( q!=NULL )
{
if( q->data == num )
{
r->link = q->link;
delete q;
return;
}

r = q;
q = q->link;
}
cout<<"\nElement "<<<" not Found.";
}

void linklist::display()
{
node *q;
cout<

for( q = p ; q != NULL ; q = q->link )
cout<<data;

}

int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;

return c;
}

linklist::~linklist()
{
node *q;
if( p == NULL )
return;

while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}

int main()
{
linklist ll;
cout<<"No. of elements = "<
ll.append(12);
ll.append(13);
ll.append(23);
ll.append(43);
ll.append(44);
ll.append(50);

ll.add_as_first(2);
ll.add_as_first(1);

ll.addafter(3,333);
ll.addafter(6,666);

ll.display();
cout<<"\nNo. of elements = "<

ll.del(333);
ll.del(12);
ll.del(98);
cout<<"\nNo. of elements = "<
return 0;
}




2) How to Find a Link List is Circular ?

To find a list is circular take 3 pointers(Lets say a, b, c)

*a = header->next
*b= c->next
*c=b->next


Check if(a==b && a==D)

Lets say the all headers are at Node A(means a=A, b=A, c=A).

1) a=A , b= B,c=C
2) a=B, b=D, c=E
3) a=C, b=F, c=B
4) a=D, b=C, c=D


Sorting

Bubble sort

First Pass:

( 5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.

( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4

( 1 4 5 2 8 ) \to ( 1 4 2 5 8 ), Swap since 5 > 2

( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Second Pass:

( 1 4 2 5 8 ) \to ( 1 4 2 5 8 )

( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

Third Pass:

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

Finally, the array is sorted, and the algorithm can terminate.



Worst case performance O(n2) Best case performance O(n) Average case performance O(n2)
Insertion Sort Advantage:
* Simple to implement
* Efficient on (quite) small data sets
* Efficient on data sets which are already substantially sorted: it runs in O(n + d) time, where d is the number of inversions
* More efficient in practice than most other simple O(n2) algorithms such as selection sort or bubble sort: the average time is n2/4 and it is linear in the best case
* Stable (does not change the relative order of elements with equal keys)
* In-place (only requires a constant amount O(1) of extra memory space)
* It is an online algorithm, in that it can sort a list as it receives it.

5 7 0 3 4 2 6 1 (0)

5 7 0 3 4 2 6 1 (0)
0 5 7 3 4 2 6 1 (2)
0 3 5 7 4 2 6 1 (2)
0 3 4 5 7 2 6 1 (2)
0 2 3 4 5 7 6 1 (4)
0 2 3 4 5 6 7 1 (1)
0 1 2 3 4 5 6 7 (6)
Performance :
Worst case performance О(n2)
Best case performance O(n)
Average case performance О(n2)


Traversing a linked List


int SinglyLinkedList::traverse() {

int sum = 0;

SinglyLinkedListNode *current = head;

SinglyLinkedListNode *previous = NULL;

while (current != NULL) {

sum += current->value;

previous = current;

current = current->next;

}

return sum;

}


Reverse a Linked List


class Node
{
public:
Node(int value) : value(value), next(NULL) {}
public:
int value;
Node* next;
};

Node* reverseList(Node* head) {
Node* newList = NULL;
Node* current = head;
while (current) {
Node* next = current->next;
current->next = newList;
newList = current;
current = next;
}

return newList;
}
 Traversing a linked list

	/* Program to illustrate traversing a list */
	#include 
	struct list {
		int         value;
		struct list *next;
	};

	main()
	{
		struct list n1, n2, n3, n4;
		struct list *list_pointer = &n1;

		n1.value = 100;
		n1.next = &n2;
		n2.value = 200;
		n2.next = &n3;
		n3.value = 300;
		n3.next = &n4;
		n4.value = 400;
		n4.next = 0;


		while( list_pointer != 0 )  {
			printf("%d\n", list_pointer->value);
			list_pointer = list_pointer->next;
		}
	}

Compute the sum of all the values in the nodes of a single linked list

public int getSum(Node head)
{
if (head == null) return 0;
else return getSum(head.next) + head.value;
}