Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct node nd;
- struct node
- {
- int id;
- char name[80];
- nd *next;
- };
- nd *head=NULL;
- void insertfirst(int id,char name[80])
- {
- nd *newnode=(nd*)malloc(sizeof(nd));
- strcpy(newnode->name,name);
- newnode->id=id;
- newnode->next=head;
- head=newnode;
- return;
- }
- void insertmiddle(int n,int id,char name[80])
- {
- nd *temp=head;
- nd *newnode=(nd*)malloc(sizeof(nd));
- strcpy(newnode->name,name);
- newnode->id=id;
- int i;
- for(i=0;i<(n-2);i++)
- {
- temp=temp->next;
- }
- nd *temp1=temp->next;
- temp->next=newnode;
- newnode->next=temp1;
- return;
- }
- void insertlast(int id,char name[80])
- {
- nd *temp=head;
- nd *newnode=(nd*)malloc(sizeof(nd));
- strcpy(newnode->name,name);
- newnode->id=id;
- newnode->next=NULL;
- if(head==NULL)
- {
- head=newnode;
- return;
- }
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- temp->next=newnode;
- return;
- }
- void delete(int id)
- {
- nd *temp=head;
- int i,count=0;
- while(temp!=NULL)
- {
- count++;
- if(temp->id==id)
- {
- break;
- }
- temp=temp->next;
- }
- if(count==1)
- {
- head=temp->next;
- return;
- }
- nd *temp2=head;
- for(i=0;i<(count-2);i++)
- {
- temp2=temp2->next;
- }
- nd *temp1=temp2->next;
- temp2->next=temp1->next;
- return;
- }
- void search(int id)
- {
- nd *temp=head;
- while(temp!=NULL)
- {
- if(temp->id==id)
- {
- printf("Number is found\n",temp->id);
- break;
- }
- temp=temp->next;
- }
- return;
- }
- void display()
- {
- nd *temp=head;
- while(temp!=NULL)
- {
- printf("Id: %d Name: %s\n",temp->id,temp->name);
- temp=temp->next;
- }
- printf("\n");
- }
- int main()
- {
- nd *temp=head;
- insertfirst(192152784,"Jamir Uddin Zidan");
- insertfirst(192152776,"Osman Goni");
- insertfirst(192152878,"Nahid Hasan");
- insertmiddle(2,192152886,"Akash Sarker");
- insertlast(192152774,"Mredul Hasan Pias");
- display();
- delete(192152776);
- display();
- search(192152784);
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement