#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct numType
{
	int num;
	struct numType *next;
}node;

typedef struct stringType
{
	char string[80];
	struct stringType *next;
}stringRecord, *stringPtr;

void main()
 {
	node *head, *pos;
	int done = 1;
	int temp;
	node *p, *q;
	stringPtr headS, posS, r,s;
	int i,m=0,n=0;

	head = (node*)malloc(sizeof(node));
	head->num = 2;
	pos = (node*)malloc(sizeof(node));
	head->next = pos;
	pos->num = 4;
	pos->next = (node*)malloc(sizeof(node));
	pos->next->num = 6;
	pos = pos->next;
	pos->next = (node*)malloc(sizeof(node));
	pos->next->num = 1;
	pos->next->next = head;
	head = pos->next;

	headS = (stringPtr)malloc(sizeof(stringRecord));
	strcpy(headS->string,"abc");
	posS = (stringPtr)malloc(sizeof(stringRecord));
	headS->next = posS;
	strcpy(posS->string,"hello");
	posS->next = (stringPtr)malloc(sizeof(stringRecord));
	posS = posS->next;
	posS->next = (stringPtr)malloc(sizeof(stringRecord));
	strcpy(posS->string,"the");
	posS = posS->next;
	posS->next = (stringPtr)malloc(sizeof(stringRecord));
	strcpy(posS->string,"Hi");
	posS = posS->next;
	posS->next = (stringPtr)malloc(sizeof(stringRecord));
	strcpy(posS->string,"surviving");
	posS = posS->next;
	posS->next = (stringPtr)malloc(sizeof(stringRecord));
	strcpy(posS->string,"string");
	posS = posS->next;
	posS->next = (stringPtr)malloc(sizeof(stringRecord));
	strcpy(posS->string,"bye");
	posS->next = headS;
	headS = posS;

	s = headS;

	posS = headS->next;
	printf("%s ", posS->string);
    posS = posS->next;
	while (posS != headS->next)
            {
				printf("%s ", posS->string);
				posS = posS->next;
            }

	do
	{
		p = head->next;
		done = 1;
		while (/*1*/p!=head)
		{
			q = p->next;
			if (p->num > q->num)
			{
				temp = p->num;
				p->num = q->num;
				q->num = temp;
				done = 0;
			}
			p = p->next;
		}
	}
	while (/*2*/done!=1);

	//print list
	pos = head->next;
    printf("%d ", pos->num);
    pos = pos->next;
	while (pos != head->next)
            {
				printf("%d ", pos->num);
				pos = pos->next;
            }
	printf("\n");
	if (head)
	{
		s = headS;
		do
		{
			p = p->next;
			n = p->num;
			for (i = m; i<n-1; i++) s = s->next;
			r = s->next;
			/*3*/s->next = r->next;
			free(r);
			m = i+1;
		}
		while (p !=head);
	}
	posS=s->next;
    printf("%s ", posS->string);
	posS = posS->next;
    while (posS != s->next)
    {
		printf("%s ", posS->string);
        posS = posS->next;
    }
     
            getch();

}