Vous êtes sur la page 1sur 2

//asgn-4 ans:1

#include<stdio.h>
#include<stdlib.h>
struct node{
int val;
struct node *next;
};
typedef struct node node;
node* new_head(){
node *n=(node*)malloc(sizeof(node));
printf("new head created successfully!\n");
return n;
}
node* create_node(int value){
node *n=(node*)malloc(sizeof(node));
n->val=value;
printf("node created successfully with value %d\n",value);
return n;
}
void list_add(node *prev_node,int value){
node *new_node =(node*)malloc(sizeof(node));
new_node->val=value;
prev_node->next=new_node;
}
void print_list(node *head){
node* temp=head;
printf("List :\n");
while(temp!=NULL){
printf("%d ",temp->val);
temp=temp->next;
}
printf("\n");
}
int main(){
node *head=new_head();
node *temp=head;
printf("please enter the number of elements in the list :\n");
int numOfTerms;
scanf("%d",&numOfTerms);
for(int i=0;i<numOfTerms;i++){
int value;
printf("\nplease enetr the %d number :",i);
scanf("%d",&value);
list_add(temp,value);
temp=temp->next;
}
print_list(head);
printf("\nplease enetr the numebr you want to delete from the
list :\n");
int toDelete;
scanf("%d",&toDelete);
if(head->val==toDelete){
head=head->next;
printf("Deleted successfully from the list!\n");
//print_list(head);
exit(0);
}
node *temp2=head;
while(1){
if(temp2==NULL){
printf("The given value cant be found in the list !\n");
}
else{
node *prev=temp2;
temp2=temp2->next;
if(temp2->val==toDelete){
node *next_node=temp2->next;
prev->next=next_node;
printf("The given value is found in the list !\n");
print_list(head);
exit(0);
}
}
}
return 0;
}

Vous aimerez peut-être aussi