freakytanmay / Semester-2

In this repository all my pratical and seminar documention
0 stars 0 forks source link

Data structure termwork #3

Open freakytanmay opened 2 years ago

freakytanmay commented 2 years ago

` //prog1 /Write a program to implement BookData using circular linked list with following operations. Node has info like Book_id, Bname, price and author. Insert from first Insert from last Insert by sorted Book_id Delete by specific Bname Delete from first Delete from last Display in ascending order by Bname Display in descending order by Bname Display list author wise/

include

include

include

struct linklist { int bid; char bname[10]; int price; char author[10]; struct linklist next; }first=NULL,curr=NULL,new;

typedef struct linklist *node;

int i; void display(); void firstin(); void lastin(); void betweenin(); void firstdel(); void lastdel(); void delbyname(); void newadd(); void bnameasd(); void bnamedse(); void authorvis();

void main() { int ch,size; node temp;

do
{
    printf("\n\t****MENU*****");
    printf("\nPress 1 For InsertFirst!");
    printf("\nPress 2 For InsertLast!");
    printf("\nPress 3 For insert by Book-Id!");
    printf("\nPress 4 For Delete with name!");
    printf("\nPress 5 For DeleteFirst!");
    printf("\nPress 6 For Deletelast!");
    printf("\nPress 7 For book list ascending!");
    printf("\nPress 8 For book list descending!");
    printf("\nPress 9 For list author wise!");
    printf("\nPress 0 For Exit!");
    printf("\n*****END*****");
    printf("\nEnter Your Choice :");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1: firstin();
                display();
                break;

        case 2: lastin();
                display();
                break;

        case 3: betweenin();
                display();
                break;

        case 4: delbyname();
                display();
                break;

        case 5: firstdel();
                display();
                break;

        case 6: lastdel();
                display();
                break;

        case 7: bnameasd();
                break;

        case 8: bnamedse();
                break;

        case 9: authorvis();
                break;

        case 0: break;

        default:printf("\n\tWrong Choise!");
                break;
    }
}while(ch!=0);

} void display() { if(first==NULL) { printf("\n\tLink-List Empty!"); return; } printf("\n\n"); curr=first; while(curr->next!=first) { printf("\n[ %x | %d | %s | %d | %s ]",curr,curr->bid,curr->bname,curr->price,curr->author); curr=curr->next; } printf("\n[ %x | %d | %s | %d | %s ]",curr,curr->bid,curr->bname,curr->price,curr->author); printf("\n\n"); }

void newadd() { new=(struct linklist*)malloc(sizeof(struct linklist)); printf("\n\tEnter Book ID : "); scanf("%d",&new->bid);

printf("\n\tEnter Book Name : ");
scanf("%s",new->bname);

printf("\n\tEnter Book price : ");
scanf("%d",&new->price);

printf("\n\tEnter Book author : ");
scanf("%s",new->author);
new->next=NULL;

}

void firstin() { if(first==NULL) { newadd(); first=new; curr=new; first->next=first; } else { newadd(); curr=first; while(curr->next!=first) { curr=curr->next; } new->next=first; first=new; curr->next=first; } }

void lastin() {

if(first==NULL)
{
    newadd();
    first=new;
    curr=new;
    first->next=first;
}
else
{
    newadd();
    curr=first;
    while(curr->next!=first)
    {
        curr=curr->next;
    }
    curr->next=new;
    new->next=first;
    curr=new;
}

}

void betweenin() { int po,c=0; node lab; newadd(); po=new->bid; if(first==NULL) { first=new; curr=new; first->next=first; }

if(po==first->bid)
{
    curr=first;
    while(curr->next!=first)
    {
        curr=curr->next;
    }
    new->next=first;
    first=new;
    curr->next=first;
    return;
}

curr=first;
while(curr->next!=first && po>curr->bid)
{
    curr=curr->next;

}

if(curr->next == first)
{
    curr->next=new;
    new->next=first;
    curr=new;
    return;
}

new->next=curr->next;
curr->next=new;

}

void delbyname() { char str[10]; int f=0; node prev; node temp; if(first==NULL) { printf("\n\tLink-List Is Empty!"); return; }

printf("\n\tEnter Book Name : ");
scanf("%s",&str);

curr=first;

while(curr->next!=first)
{
    printf("%s == %s %d",curr->bname,str,strcmp(curr->bname,str));
    if(!strcmp(curr->bname,str))
    {
        f=1;
        temp=curr;
        break;
    }
    else
    {
        prev=curr;
        curr=curr->next;
    }
}
if(f==1)
{
    if(curr==first)   
    {
        firstdel();
        return;
    }    

    prev->next=curr->next;
    printf("\n\t[ %d | %s | %d | %s ] Is deleted!",temp->bid,temp->bname,temp->price,temp->author);
    free(temp);
}
else
{
    curr=curr->next;
    if(!strcmp(curr->bname,str))
    {
        lastdel();
    }
    else
    {
        printf("Book is not Found!!!");
    }

}

}

void firstdel() { node temp; if(first==NULL) { printf("\n\tLink-List Is Empty!"); return; }

curr=first;
while(curr->next!=first)
{
    curr=curr->next;
}
temp=first;
first=first->next;
curr->next=first;
printf("\n\t[ %d | %s | %d | %s ] Is deleted!",temp->bid,temp->bname,temp->price,temp->author);
free(temp);

}

void lastdel() { node temp; node prev; if(first==NULL) { printf("\n\tLink-List Is Empty!"); return; } curr=first; while(curr->next!=first) { prev=curr; curr=curr->next; } temp=curr; prev->next=curr->next; printf("\n\t[ %d | %s | %d | %s ] Is deleted!",temp->bid,temp->bname,temp->price,temp->author); free(temp); }

void bnameasd() {
node index = NULL;
int tid,tprice; char tname[10],tauthor[10];

if(first == NULL) {  
    return;  
}  
else { 

    curr=first;
    while(curr->next!= first) 
    {  
        printf("yes");
        index = curr->next;  
        while(index!= first) 
        {  
            printf("%c > %c",curr->bname[0],index->bname[0]);
            if(curr->bname[0] > index->bname[0]) 
            {  
                tid = curr->bid;
                strcpy(tname,curr->bname);
                tprice = curr->price;
                strcpy(tauthor,curr->author);

                curr->bid = index->bid;
                strcpy(curr->bname,index->bname);
                curr->price = index->price;
                strcpy(curr->author,index->author);

                index->bid = tid;
                strcpy(index->bname,tname);
                index->price = tprice;
                strcpy(index->author,tauthor);

            }  
            index = index->next;  
        }  
        curr = curr->next;  
    }      
}  
display();

}

void bnamedse() {
node index = NULL;
int tid,tprice; char tname[10],tauthor[10];

if(first == NULL) {  
    return;  
}  
else { 

    curr=first;
    while(curr->next!= first) 
    {  
        printf("yes");
        index = curr->next;  
        while(index!= first) 
        {  
            printf("%c > %c",curr->bname[0],index->bname[0]);
            if(curr->bname[0] < index->bname[0]) 
            {  
                tid = curr->bid;
                strcpy(tname,curr->bname);
                tprice = curr->price;
                strcpy(tauthor,curr->author);

                curr->bid = index->bid;
                strcpy(curr->bname,index->bname);
                curr->price = index->price;
                strcpy(curr->author,index->author);

                index->bid = tid;
                strcpy(index->bname,tname);
                index->price = tprice;
                strcpy(index->author,tauthor);

            }  
            index = index->next;  
        }  
        curr = curr->next;  
    }      
}  
display();

}

void authorvis() {
node index = NULL;
int tid,tprice; char tname[10],tauthor[10];

if(first == NULL) {  
    return;  
}  
else { 

    curr=first;
    while(curr->next!= first) 
    {  
        printf("yes");
        index = curr->next;  
        while(index!= first) 
        {  
            printf("%c > %c",curr->bname[0],index->bname[0]);
            if(curr->author[0] > index->author[0]) 
            {  
                tid = curr->bid;
                strcpy(tname,curr->bname);
                tprice = curr->price;
                strcpy(tauthor,curr->author);

                curr->bid = index->bid;
                strcpy(curr->bname,index->bname);
                curr->price = index->price;
                strcpy(curr->author,index->author);

                index->bid = tid;
                strcpy(index->bname,tname);
                index->price = tprice;
                strcpy(index->author,tauthor);

            }  
            index = index->next;  
        }  
        curr = curr->next;  
    }      
}  
display();

}`