Sunteți pe pagina 1din 9

#include<stdio.

h>
#include<conio.h>

struct KaviList
{
int data;
struct KaviList *next;
};

typedef struct KaviList node;

node *Create(node *p);
void Display(node *p);
int Size(node *p);
int Sum(node *p);
node *RemoveLast(node *p);
node *ListCopy(node *head,node *copyhead);
node *SubList(node *head,node *subhead,int a, int b);
node *Append(node *head, node *apphead);
node *Concat(node *head, node *secondhead, node *concathead);
node *Replace(node *head);
node *Swap(node *head, int a,int b);
node *Sort(node *head);
node *Merge(node *head, node *sechead, node *mergehead);

int main()
{
node *head;
head=NULL;

node *copyhead;
copyhead=NULL;

node *subhead;
subhead=NULL;

node *apphead;
apphead=NULL;

node *secondhead, *concathead;
secondhead=NULL;
concathead=NULL;

node *sechead, *mergehead;
sechead=NULL;
mergehead=NULL;

int choice;
int subnum_1,subnum_2;
int pos_1,pos_2;

while(choice!=99){
printf("\n\n1 : To Create List");
printf("\n2 : To Display/Print List");
printf("\n3 : Size Of List");
printf("\n4 : Sum Of Elements In List");
printf("\n5 : Remove Last Element");
printf("\n6 : Copy Of Link List");
printf("\n7 : Sublist");
printf("\n8 : Append");
printf("\n9 : Concatenation");
printf("\n10 : Replace");
printf("\n11 : Swap");
printf("\n12 : Sort");
printf("\n13 : MergeSort");
printf("\n99 : To Exit\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
head=Create(head);
break;
case 2:
Display(head);
break;
case 3:
printf("Size Of List Is : %d",Size(head));
break;
case 4:
printf("Sum of all the elements in list is : %d",Sum(head));
break;
case 5:
head=RemoveLast(head);
break;
case 6:
copyhead=ListCopy(head,copyhead);
break;
case 7:
if(head!=NULL){
printf("Enter starting position : ");
scanf("%d",&subnum_1);
printf("Enter ending position : ");
scanf("%d",&subnum_2);
subhead=SubList(head,subhead,subnum_1,subnum_2);
}else{
printf("Original List is Empty ");
}
break;
case 8:
printf("Enter the entries of new List\n");
apphead=Create(apphead);
printf("Original List has been appended");
head=Append(head,apphead);
break;
case 9:
printf("Enter the entries for new List\n");
secondhead=Create(secondhead);
concathead=Concat(head,secondhead,concathead);
break;
case 10:
head=Replace(head);
break;
case 11:
printf("Enter the first position : ");
scanf("%d",&pos_1);
printf("Enter the second position : ");
scanf("%d",&pos_2);
head=Swap(head,pos_1,pos_2);
break;
case 12:
head=Sort(head);
break;
case 13:
printf("Enter elements for second list \n");
sechead=Create(sechead);
mergehead=Merge(head,sechead,mergehead);
break;
}

}
return 0;
}

int Size(node *p)
{
int counter =0;

while(p!=NULL){
counter++;
p=p->next;
}
return counter;
}

int Sum(node *p)
{
int Summation=0;
while(p!=NULL){
Summation=Summation+p->data;
p=p->next;
}
return Summation;
}

node *Create(node *p)
{
char choice;
p=(node *)malloc(sizeof(node));
node *q;

q=p;

while(choice!='N'){
printf("Enter the data to be inserted : ");
scanf("%d",&p->data);
printf("Do you want to continue : 'Y' or 'N'\n");
choice=getch();

if(choice=='Y'){
p->next=(node *)malloc(sizeof(node));
p=p->next;
}else{
p->next=NULL;
p=q;
}
}
return p;
}

void Display(node *p)
{
if(p==NULL){
printf("Linked List is empty");
}
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
}

node *RemoveLast(node *p)
{
node *q;
q=p;

if(p==NULL){
printf("List is Empty");
}
else if(Size(p)<2){// if there are less then two elements in list then we wont remove last node/ last
value
printf("List Must have at least two elements. Right now there is %d element.",Size(p));
}
else{
while(p->next->next!=NULL){// this while loop will give me hold of second last element
p=p->next;
}
free(p->next);//next node of second last node is freed from memory
p->next=NULL;//the next address of second last node is made to NULL. Thus now, our second
last node has become last node
}
p=q;
return p;
}

node *ListCopy(node *head, node *copyhead)
{
node *temp;

if(head!=NULL){
copyhead=(node *)malloc(sizeof(node));
temp=copyhead;
while(head->next!=NULL){
copyhead->data=head->data;
copyhead->next=(node *)malloc(sizeof(node));
copyhead=copyhead->next;
head=head->next;
}
copyhead->data=head->data;
copyhead->next=head->next;
copyhead=temp;
Display(copyhead);
return copyhead;
}else{
printf("Original List is empty");
}

}

node *SubList(node *head,node *subhead,int a,int b)
{
int count=0;
node *temp;

if(Size(head)<(b+1)){
printf("Length of original list is less then the specified end point");
}else{
subhead=(node *)malloc(sizeof(node));
temp=subhead;
while(count<(b-1)){ // through this while loop we will get second last element of our sublist
if(count>=a && count<(b-1)){
subhead->data=head->data;
subhead->next=(node *)malloc(sizeof(node));
subhead=subhead->next;

}
head=head->next;
count++;
}

subhead->data=head->data; //we get the last element of our sublist
subhead->next=NULL;

subhead=temp;
Display(subhead);
return subhead;
}
}

node *Append(node *head, node *apphead)
{
node *temp;

if(head==NULL){
head=apphead;
}else{
temp=head;

while(head->next!=NULL){
head=head->next;
}
head->next=apphead;

head=temp;
}
printf("\nThe new list is as it is : ");
Display(apphead);
return(head);
}

node *Concat(node *head, node *secondhead, node *concathead)
{
node *temp;
node *sectemp;
node *hedtemp;

sectemp=secondhead;
hedtemp=head;

concathead=(node *)malloc(sizeof(node));
temp=concathead;

while(head!=NULL){
concathead->data=head->data;
concathead->next=(node *)malloc(sizeof(node));
concathead=concathead->next;
head=head->next;
}
concathead->next=secondhead;

while(secondhead->next!=NULL){
concathead->data=secondhead->data;
concathead->next=(node *)malloc(sizeof(node));
concathead=concathead->next;
secondhead=secondhead->next;
}
concathead->data=secondhead->data;
concathead->next=secondhead->next;


concathead=temp;
secondhead=sectemp;
head=hedtemp;
printf("\nOriginal list is as it is : ");
Display(head);
printf("\nSecond List is as it is : ");
Display(secondhead);
printf("\nConcated List consist of original list and second list : ");
Display(concathead);
return concathead;

}

node *Replace(node *head)
{
node *temp;

int position, newval;
int count=0;

if(head==NULL){
printf("List is empty ,therefore no elements can be replaced");
}else{
temp=head;
printf("Enter the position to be replaced : ");
scanf("%d",&position);

if(Size(head)<(position+1)){
printf("The position you mention is beyond the size of List. Size of List is : %d",Size(head));
}else{
printf("Enter the new value : ");
scanf("%d",&newval);

while(head!=NULL){
if(count==position)
{
head->data=newval;
break;
}
head=head->next;
count++;
}
head=temp;
printf("\nValue of Original list has been changed.\nView the changed list by selecting, Option
2 : Display");
return head;
}
}
}

node *Swap(node *head,int pos_1, int pos_2)
{
int counter=0;
int looper=0;

node *temp;
node *firstadd; // stores the address of first position
int firstvalue; // stores the value of first position

if(head==NULL){
printf("List is empty, values cant be swapped");
}else{
temp=head;

if((Size(head))<(pos_2)){
printf("The positions are not within the size of list. The size of the list is : %d",Size(head));
}else{
while(counter!=pos_1){
head=head->next;
counter++;
}
firstadd=head; // stores the address of first position
firstvalue=head->data; // stores the value of first position

head=temp;
while(looper!=pos_2){
head=head->next;
looper++;
}
firstadd->data=head->data;
head->data=firstvalue;

head=temp;
return head;
}
}
}

node *Sort(node *head)
{
int k,count;
int length;
length=Size(head);
node *q;
q=head;
for(count=0;count<length;count++){ //one while loop will put maximum number at last, as we
have total of 'length' numbers, we need that many while loop to adjust or keep large numbers at
end
head=q;
// for example inputs are 1 3 6 15 8 11 9 2 , then in one while loop no.
//15 will be brought to last position,
//in another while loop no. 11 will be brought or placed to second last position, then no. 9, then no.8
//and so on.
while(head->next!=NULL){ // while loop will put the maximum number at last
if(head->data > head->next->data){
k=head->data;
head->data=head->next->data;
head->next->data=k;
}
head=head->next;
}
}
head=q;
return(head);
}

node *Merge(node *head, node *sechead, node *mergehead)
{
mergehead=Concat(head,sechead,mergehead);
mergehead=Sort(mergehead);

printf("Merged and sorted list is :\n");
Display(mergehead);
return mergehead;

}

S-ar putea să vă placă și