Linked List Program
can we optimize it? how???
// playlist of songs and different operations apply on list
#include<stdio.h>
#include<conio.h>
struct node* reverse_list(struct node *);
struct node* create_list(struct node *,int,char *);
struct node* insert_node(struct node *,int,int,char *);
struct node* del_node(struct node *,int);
void display(struct node *);
struct node* search(struct node*,char *);
char* match(char*,char*);
struct node
{
char **p;
struct node *next;
};
main()
{ struct node *head=NULL;
char *a="love song";
char *List[8]={"song1 of the list","music of the best","sangeet of hindi",
"listsong of all","sadsong of heart",
"marriage song with boom!!","love song","old song"};
int i,num,position,choice;
clrscr();
for(i=0;i<8;i++)
{
printf("%d\t%s\n",i+1,List[i]);
}
printf("create a link list of songs\n");
do
{
printf("enter the serial number for add in list\n");
scanf("%d",&num);
if((num<1)||(num>8))
continue;
head=create_list(head,num,List);
printf("for exit press 0 n 1 for continue..\n");
scanf("%d",&i);
}while(i);
printf("your play list is\n");
display(head);
while(1)
{printf("*******Menu********\n");
printf("1. add a song in list\n2. delete a song from list\n3. reverse List\n");
printf("4. exit\n5. search\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("add a song in list\n enter the position\nand serial number\n");
scanf("%d%d",&position,&num);
if((num<1)||(num>8))
{printf("try with right number!");
break;
}
head=insert_node(head,num,position,List);
display(head);
break;
case 2:
printf("delete a song in list\n enter the position\n");
scanf("%d",&position);
head=del_node(head,position);
display(head);
break;
case 3:
head=reverse_list(head);
display(head);
break;
case 4:
return 0;
break;
case 5:
head=search(head,a);
printf("%s\n",*head->p);
break;
}
}
getch();
return 0;
}
// function to create a new list (base address of playlist,serial no. of song,base address of Main default list )
struct node* create_list(struct node *head,int num,char *List)
{
struct node *new_node,*ptr;
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
printf("no Free space\n");
else if(head==NULL)
{ new_node->p=List+(2*num-2); // to insert the base address of that perticular song in link list
head=new_node;
head->next=NULL;
}
else
{
new_node->p=List+(2*num-2);
for(ptr=head;ptr->next!=NULL;ptr=ptr->next);
{
}
ptr->next=new_node;
new_node->next=NULL;
}
return head; // return base address of playlist
}
void display(struct node *head)
{ struct node *ptr1;
int i;
printf("********your list is********\n");
for(ptr1=head,i=1;;ptr1=ptr1->next,i++)
{
printf("%d\t%s\n",i,*ptr1->p);
if(ptr1->next==NULL)
break;
}
}
// function to add a song at perticular position with (base address link list,serial no. of song from default,position to add in link list,default list base address)
struct node* insert_node(struct node *head,int num,int position,char *List)
{
struct node *new_node,*ptr;
int i;
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
printf("no Free space\n");
else if(position==1)
{ new_node->p=List+(2*num-2);
new_node->next=head;
head=new_node;
}
else
{
new_node->p=List+(2*num-2);
ptr=head;
for(i=1;i<position-1;ptr=ptr->next,i++);
{
}
new_node->next=ptr->next;
ptr->next=new_node;
}
return head;
}
struct node* del_node(struct node *head, int position)
{ int i;
struct node *ptr;
ptr=head;
if(ptr->next==NULL)
{ head=NULL;
printf("link list empty\n");
return (head);
}else if(position==1)
{
head=head->next;
return head;
}
else
{
for(i=1;i<position-1;i++)
{
ptr=ptr->next;
}
ptr->next=(ptr->next)->next;
return head;
}
}
struct node* reverse_list(struct node *head)
{
struct node *ptr3,*temp,*ptr1,*ptr2,*ptr;
int i, count;
temp=head; //to break connection of Head with first default node
for(i=1,ptr=head;ptr->next!=NULL;i++)
{
ptr=ptr->next;
} // found the last node n no. node in list
count=i;
head=NULL; // for empty reverse list
while(count>1) // no. of time operation occure for taking last node n put at front places to reverse a list
{
ptr1=temp;
ptr2=temp->next;
while(ptr2->next!=NULL) // to take off one by one node from last
{
ptr1=ptr1->next;
ptr2=ptr2->next;
}
ptr1->next=NULL; //to make last node of list after take off node
if(head==NULL) // to add first node of reverse list
{
head=ptr2;
head->next=NULL;
}
else{ //to add one by one nodes in reverse list
for(ptr3=head;ptr3->next!=NULL;ptr3=ptr3->next)
{
}
ptr3->next=ptr2;
ptr2->next=NULL;
}
count--;
if(count<2) // to close up last node of reverse list
{
ptr2->next=temp;
temp->next=NULL;
}
} // end while loop
return head; // return head of reverse list
}
thanks
have a good day!!
Comments
Post a Comment