EmbLogic's Blog

C assignment link list operations Q1(pass by value method)

HEADER.h

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node * next;

};
int get_choice();
struct node * operation(int, struct node *);
struct node * create_node(struct node *);
struct node * insert_node(struct node*);
struct node * insert_beg(struct node*);
struct node * insert_end(struct node*);
struct node * insert_nth(struct node*);
struct node * insert_key(struct node*);
struct node * delete_node(struct node*);
struct node * del_beg(struct node *);
struct node * del_end(struct node *);
struct node * del_nth(struct node *);
struct node * del_key(struct node *);
struct node * display(struct node*);
struct node * sorting(struct node *);
struct node * sort_ascend(struct node *);
struct node * sort_descend(struct node *);
struct node * destroy(struct node *);

MAIN

#include”header.h”
int main()
{
struct node  * start;
start =  NULL;
int choice;
while(1)
{        choice = get_choice();
start=operation(choice,start);
}

}

FUNCTON DEFINITIONS

#include”header.h”
int flag=0;
int get_choice()                                        //getchoice from user
{
int choice;
printf(“\n—————_MENU————–\n”);
printf(”  1. CREATE LINK LIST\n”);
printf(”  2. INSERT\n”);
printf(”  3. DELETE\n”);
printf(”  4. SORT\n”);
printf(”  5. DISPLAY\n”);
printf(”  6. DESTROY\n”);
printf(”  0. Exit\n”);
printf(“\n\n  Enter your choice…\n”);

scanf(“%d”,&choice);
return choice;
}

struct node * operation(int choice,struct node *start )                    //operations
{

struct node * temp;
switch(choice)
{
case 1: temp=create_node(start);
break;
case 2: temp=insert_node(start);
break;
case 3: temp=delete_node(start);
break;
case 4: temp = sorting(start);
break;
case 5: temp=display(start);
break;
case 6: temp = destroy(start);
free(temp);
break;
case 0: exit(0);
default:printf(“\nINVALID OPTION”);

}return temp;

}

struct node * create_node(struct node *start)                            //create first node
{

if(flag)
{ printf(“link list is already created\n”);
}
if(flag==0)
{
start=(struct node*)malloc (sizeof(struct node));
printf(“enter the info of link list \n”);
scanf(“%d”, &(start->info));
start->next = NULL;
flag = 1;
printf(“link list created successfully\n”);
printf(“first element is %d\n”, start->info);
return start;

}
}

struct node * insert_node(struct node *start)                            //insert node menu
{
struct node *temp1;

if(flag==0)
printf(“linked list not created!\n”);
else
{
int choice;
printf(“___________INSERT MENU_______________\n”);
printf(“1. INSERT AT BEGGINING\n”);
printf(“2. INSERT AT END\n”);
printf(“3. INSERT AT NTH\n”);
printf(“4. INSERT AFTER KEY VALUE\n”);
printf(“\n\nEnter your choice…\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:temp1 = insert_beg(start);
break;

case 2:temp1 = insert_end(start);
break;

case 3:temp1 = insert_nth(start);
break;

case 4:temp1 = insert_key(start);
break;
default : printf(“\nINVALID”);
}
return temp1;
}
}
struct node * insert_beg(struct node *start)                                //insert at begining
{
struct  node *b;
b=(struct node *)malloc(sizeof(struct node));
printf(“enter the element of node\n”);
scanf(“%d”,&(b->info));
b->next = start;
start = b;
return start;

}

struct node * display(struct node *start)                                //display
{
int num=0;
struct node *new,*temp;
temp=start;
new = start;
if(flag==0)
{    printf(“\nlink list is empty!!!\n”);
}
while(temp)
{    temp=temp->next;
num++;
}
//    printf(“\nthe number of nodes are %d”,num);
if(flag)
{
if(num==0)
{    printf(“\nList is empty !!!!”);
return start;
}
else
{    printf(“the link list is —>>”);
do
{
printf(” %d “, new->info);
new = new -> next;
}while(new!=NULL);
return start;
}
}
}

struct node * insert_end(struct node *start)                                //insert at end
{
struct node *b, *n;
n = start;
b=(struct node *)malloc(sizeof(struct node));
printf(“enter the info of node\n”);
scanf(“%d”,&(b->info));
printf(“\nthe last element is %d”,b->info);

while(n->next)
{     n=n->next;
}
n->next = b;
b->next = NULL;
return start;
}

struct node * insert_nth(struct node *start)                                //insert at nth node
{
int pos,i;
struct node *temp,*new,*b;
new=(struct node *)malloc(sizeof(struct node));
temp = start;
printf(“enter the position you want to insert at \n”);
scanf(“%d”,&pos);
for(i=1;i<pos-1;i++)
{     temp    = temp ->next;
}
b=temp->next;
printf(“enter the info of node\n”);
scanf(“%d”,&(new->info));
temp->next=new;
new->next=b;

printf(“link added successfully\n”);
return start;
}

struct node * insert_key(struct node *start)                                //insert after key
{
int key;
struct node *new,*temp,*b;
temp=start;
new=(struct node *)malloc(sizeof(struct node));
printf(“enter the key value \n”);
scanf(“%d”,&key);
while(key!= (temp->info))
{    temp=temp->next;
}
printf(“enter the info of the node\n”);
scanf(“%d”,&(new->info));
b=temp->next;
temp->next=new;
new->next=b;
return start;
}

struct node * delete_node(struct node *start)                                //delete node menu
{
int choice;
struct node *temp;

if(flag==0)
printf(“linked list empty!!!\n”);
else
{
printf(“———-DELETE MENU———–\n”);
printf(“1. Delete from begining\n”);
printf(“2. Delete from end\n”);
printf(“3. Delete nth node \n”);
printf(“4. Delete after key value\n”);
printf(“\n\nEnter your choice\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1 : temp = del_beg(start);
break;
case 2 : temp = del_end(start);
break;
case 3 : temp = del_nth(start);
break;
case 4 : temp = del_key(start);
break;
default : printf(“\nINVALID”);
}
return temp;
}
}

struct node *del_beg(struct node *start)                                //delete menu
{
struct node *temp;
temp=start;
int num=0;
while(temp)
{    temp= temp->next;
num++;
}

if(num==0)
printf(“nothing to delete\n”);
else
{    start = start->next;
return start;
}
}

struct node *del_end(struct node *start)                                //delete from end
{
struct node *temp;
temp = start;
int num=0;
while(temp)
{    temp= temp->next;
num++;
}
temp=start;
if(num==0)
{    printf(“\nnothing to delete”);
}
else
{    while(temp->next->next)
{    temp=temp->next;
}
temp->next=NULL;
return start;
}
}

struct node * del_nth(struct node *start)                                //delete nth node
{
int pos,i;
struct node *temp,*b;
temp = start;
int num=0;
while(temp)
{    temp= temp->next;
num++;
}
temp=start;
if(num==0)
printf(“\nnothing to delete”);
else
{    printf(“enter the position\n”);
scanf(“%d”,&pos);
for(i=1;i<pos;i++)
{    temp = temp->next;
}
b = temp->next;
temp=start;
for(i=1;i<pos-1;i++)
{    temp = temp->next;
}
temp->next=b;
return start;
}
}

struct node * del_key(struct node *start)                                //delete at key value
{
int key,num=0;
struct node *temp,*b;
temp=start;
if(num==0)
printf(“\nnothing to delete”);
else
{    printf(“enter the node to delete\n”);
scanf(“%d”,&key);
while(temp->info != key)
{    temp = temp->next;
}
if(!temp)
{    printf(“\nthe entered key not found”);
return start;
}
b=temp->next;
temp=start;
while(temp->next->info != key)
{    temp=temp->next;
}
temp->next=b;
return start;
}

}
struct node * sorting(struct node * start)                                //sorting menu
{
int choice;
struct node *temp;
printf(“\n————-SORTING MENU————–”);
printf(“\n1. sort ascending”);
printf(“\n2. sort descending”);
printf(“\n\nenter your choice”);
scanf(“%d”,&choice);
switch(choice)
{    case 1 : temp = sort_ascend(start);
break;
case 2 : temp = sort_descend(start);
break;
default : printf(“\nINVALID”);
}
return temp;

}

struct node * sort_ascend(struct node *start)                                 //sorting ascending order(bubble sort)
{
int count=0,j,i;
struct node *temp,*b;
b=(struct node *)malloc(sizeof(struct node ));
b->next=NULL;
temp = start;
while(temp)
{    temp=temp->next;
count++;
}
temp=start;
printf(“\nthe no of nodes are %d”,count);
if(!count)
printf(“\nThe Link list is empty!!!”);
else
{
for(j=0;j<count;j++)
{
for(i=0;i<count-j-1;i++)
{    if((temp->info) > (temp->next->info) )
{    b->info = temp->info;
temp->info = temp->next->info;
temp->next->info = b->info;
temp=temp->next;

}
else
temp=temp->next;
}
temp=start;
}
return start;
}
}

struct node * sort_descend(struct node *start)                                //sorting descending order(bubble sort)
{

int count=0,j,i;
struct node *temp,*b;
b=(struct node *)malloc(sizeof(struct node ));
b->next=NULL;
temp = start;
while(temp)
{    temp=temp->next;
count++;
}
temp=start;
printf(“\nthe no of nodes are %d”,count);
if(!count)
printf(“The Link list is empty!!!!”);
else
{
for(j=0;j<count;j++)
{
for(i=0;i<count-j-1;i++)
{    if((temp->info) < (temp->next->info) )
{    b->info = temp->info;
temp->info = temp->next->info;
temp->next->info = b->info;
temp=temp->next;

}
else
temp=temp->next;
}
temp=start;
}
return start;
}
}

struct node * destroy(struct node * start)                                //destroy link list
{
start=NULL;
printf(“\nThe Link List has been Destroyed”);
return start;

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>