Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct data
- {
- int a;
- char ch;
- struct data *next;
- } data;
- data *head;
- void insert_at_first(int an,char chn)
- {
- data *n=(data*)malloc(sizeof(data));
- n->a=an;
- n->ch=chn;
- n->next=NULL;
- if(head==NULL)
- {
- head=n;
- n->next=NULL;
- }
- else
- {
- n->next=head;
- head=n;
- }
- }
- void insert_at_last(int an,char chn)
- {
- data *n=(data*)malloc(sizeof(data));
- n->a=an;
- n->ch=chn;
- n->next=NULL;
- data *list=head;
- while(list->next!=NULL)
- {
- list=list->next;
- }
- list->next=n;
- }
- void insert_at_nth()
- {
- data *n=(data*)malloc(sizeof(data));
- scanf("%d",&n);
- data *list=head;
- if(head==NULL)
- {
- head=n;
- n->next=NULL;
- }
- else if(n==1)
- {
- n->next=head;
- head=n;
- }
- else
- {
- n=n-2;
- while (n!=0 && list->next!=NULL )
- {
- list=list->next;
- n--;
- }
- n->next=list->next;
- list->next=n;
- }
- }
- void display()
- {
- data *list=head;
- while (list!=NULL)
- {
- printf("A: %d\n",list->a);
- printf("ch: %d\n",list->ch);
- list=list->next;
- }
- }
- void search()
- {
- int n;
- data *list=head;
- scanf("%d",&n);
- while(list!=NULL)
- {
- if(list->a==n)
- return 1;
- else
- list=list->next;
- }
- }
- void delete_by_pos()
- {
- int n;
- data *temp;
- data *list=head;
- scanf("%d",&n);
- if(head==NULL)
- {
- printf("no data found\n");
- }
- else if(n==1)
- {
- list=head;
- head=list->next;
- free(list);
- }
- else
- {
- n=n-2;
- while(n!=0)
- {
- list=list->next;
- }
- temp=list->next;
- list->next=temp->next;
- free(temp);
- }
- }
- void delete_by_value()
- {
- int n;
- data *temp;
- data *list=head;
- scanf("%d",&n);
- if(head==NULL)
- {
- printf("no data found\n");
- }
- else if(list->a==n)
- {
- temp=head;
- head=temp->next;
- free(temp);
- }
- else
- {
- while(list->next->a!=n)
- {
- list=list->next;
- }
- temp=list->next;
- list->next=temp->next;
- free(temp);
- }
- }
- int main ()
- {
- head=NULL;
- insert_at_first(5,'x');
- insert_at_first(6,'y');
- insert_at_first(7,'z');
- display();
- search();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement