Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program in C to create a linear linked list in memory to display its contents + delete the last element...
- #include <stdio.h>
- #include <stdlib.h>
- struct list{
- int data;
- struct list *next;
- };
- typedef struct list node;
- void display_linked_list(node *start){
- printf("\nthe linked list is : \n");
- while(start != NULL){
- fflush(stdout);
- printf("%d ",start->data);
- start = start->next;
- }
- }
- void delete_from_linked_list(node *start){
- node *temp;
- temp = start;
- while(temp->next->next != NULL){
- temp = temp->next;
- }
- free(temp->next);
- temp->next = NULL;
- display_linked_list(start);
- }
- int main(){
- node *start,*prev,*current;
- char ans;
- start = NULL;
- while(1){
- printf("do you want to add a node : Y/N \n");
- fflush(stdin);
- scanf("%c",&ans);
- if(ans == 'Y'){
- if(start == NULL){
- start = (node*)malloc(sizeof(node));
- printf("enter data part value : \n");
- fflush(stdin);
- scanf("%d",&start->data);
- start->next = NULL;
- prev = start;
- }
- else{
- current = (node*)malloc(sizeof(node));
- printf("enter data part value : \n");
- fflush(stdin);
- scanf("%d",¤t->data);
- current->next = NULL;
- prev->next = current;
- prev = current;
- }
- }
- else
- break;
- }
- display_linked_list(start);
- printf("\nafter deletion : \n");
- delete_from_linked_list(start);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement