Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct student
- {
- int id;
- char name[20];
- struct student *next;
- }*start=NULL,*current,*ne;
- int search(int a)
- {
- struct student *c;
- c=start;
- int po=0;
- while(c!=NULL)
- {
- po++;
- if(c->id==a)
- {
- return po;
- }
- c=c->next;
- }
- return -1;
- }
- void cre()
- {
- if(start==NULL)
- {
- ne=(struct student*)malloc(1*sizeof(struct student));
- printf("\nEnter id:\n");
- scanf("%d",&ne->id);
- printf("\nEnter Name:\n");
- scanf("%s",&ne->name);
- ne->next=NULL;
- start=ne;
- current=ne;
- }
- else
- {
- ne=(struct student*)malloc(1*sizeof(struct student));
- printf("\nEnter id:\n");
- scanf("%d",&ne->id);
- printf("\nEnter Name:\n");
- scanf("%s",&ne->name);
- ne->next=NULL;
- current->next=ne;
- current=ne;
- }
- }
- void dis()
- {
- struct student *c;
- c=start;
- while(c!=NULL)
- {
- printf("\n Name: %s \n ID: %d\n",c->name,c->id);
- c=c->next;
- }
- }
- int main()
- {
- int x;
- while(5)
- {
- printf("\nPress 1 For cre, 2 for Dis,3 search\n");
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- cre();
- break;
- }
- case 2:
- {
- dis();
- break;
- }
- case 3:
- {
- printf("Enter Id:\n");
- int n;
- scanf("%d",&n);
- int pos = search(n);
- if(pos==-1)
- {
- printf("\n Not Found\n");
- }
- else
- {
- struct student *b;
- b=start;
- int j;
- for(j=0;j<pos-1;j++)
- {
- b=b->next;
- }
- printf("\nName: %s\n",b->name);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement