Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // single linklist
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct st_info
- {
- char name[50];
- int age;
- struct st_info *next;
- } data;
- data *head = NULL;
- data *list = NULL;
- display()
- {
- list = head;
- while(list != NULL)
- {
- printf("%s\n",list -> name);
- printf("%d\n",list -> age);
- list = list -> next;
- }
- return;
- }
- void make_node(int n)
- {
- int i;
- for(i=0; i<n; i++)
- {
- data *N = (data*)malloc(sizeof(data));
- printf("Enter your name: ");
- scanf(" %[^\n]s",N -> name);
- printf("Enter your age: ");
- scanf("%d",&N -> age);
- N -> next = NULL;
- if(head == NULL)
- {
- head = N;
- list = head;
- }
- else
- {
- list -> next = N;
- list = list -> next;
- }
- }
- display();
- }
- void add_in_last()
- {
- list = head;
- data *p = (data*)malloc(sizeof(data));
- printf("Enter a name: ");
- scanf(" %[^\n]s",p -> name);
- printf("Enter a age: ");
- scanf("%d",&p -> age);
- p -> next = NULL;
- while(list -> next!= NULL)
- {
- list = list -> next;
- }
- list -> next = p;
- display();
- }
- void insert_at_first()
- {
- data *q = (data*)malloc(sizeof(data));
- printf("Enter your name: ");
- scanf(" %[^\n]s",q -> name);
- printf("Enter your age: ");
- scanf("%d",&q -> age);
- list = head;
- if(head == NULL)
- {
- head = q;
- }
- else
- {
- q -> next = head;
- head = q;
- }
- display();
- }
- void insert_in_nth(int n)
- {
- data * temp = NULL;
- int count = 0;
- list = head;
- data *r = (data*)malloc(sizeof(data));
- printf("Enter a name: ");
- scanf(" %[^\n]s",r -> name);
- printf("Enter a age: ");
- scanf("%d",&r -> age);
- r -> next = NULL;
- if(head == NULL)
- {
- head = r;
- }
- else
- {
- while(list != NULL)
- {
- temp = list -> next;
- count++;
- if(count == n)
- {
- list -> next = r;
- r -> next = temp;
- }
- list = list -> next;
- }
- }
- }
- void delete_by_pos(int n)
- {
- int i;
- data *list =head;
- if(n==1)
- {
- head = list ->next;
- free(list);
- return;
- }
- for(i=1; i<n-1; i++)
- {
- list = list -> next;
- }
- data *temp = list -> next;
- list -> next = temp -> next;
- free(temp);
- }
- void delete_by_valu(int n)
- {
- data *list = head;
- data *temp1=list;
- while(list!=NULL)
- {
- if(head -> age ==n)
- {
- head = temp1 -> next;
- free(temp1);
- return;
- }
- else
- {
- if(list -> age == n)
- {
- data *temp2 = temp1 -> next;
- temp1 -> next = temp2 -> next;
- free(temp2);
- return;
- }
- temp1 = list;
- list = list -> next;
- }
- }
- }
- void search_by_pos(int n)
- {
- data *list = head;
- int i;
- for(i=1; i<n; i++)
- {
- list = list -> next;
- }
- printf("%s\n",list -> name);
- printf("%d\n",list -> age);
- }
- void search_by_valu(int n)
- {
- data *list = head;
- while(list !=NULL)
- {
- if(list -> age = n )
- {
- printf("%s\n",list -> name);
- printf("%d\n",list -> age);
- return;
- }
- else
- list = list -> next;
- }
- }
- main()
- {
- int n,m,x,y,z,h;
- printf("Enter you node amount: ");
- scanf("%d",&n);
- printf(".............\n");
- make_node(n);
- printf(".............\n");
- add_in_last();
- printf(".............\n");
- insert_at_first();
- printf(".............\n");
- printf("Enter position number to add a node: ");
- scanf("%d",&m);
- insert_in_nth( m);
- printf(".............\n");
- display();
- printf(".............\n");
- printf("Enter a position to delete: ");
- scanf("%d",&x);
- delete_by_pos(x);
- printf(".............\n");
- display();
- printf("Enter a value to delete: ");
- scanf("%d",&y);
- delete_by_valu(y);
- printf(".............\n");
- display();
- printf(".............\n");
- printf("Enter a position to search: ");
- scanf("%d",&z);
- search_by_pos(z);
- printf(".............\n");
- printf("Enter a value to search: ");
- scanf("%d",&h);
- search_by_valu(h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement