Answer to Question #232419 in C for Vishnukumar

Question #232419
Create a list which contains character and then perform insert, delete, display, search and exit operations.
1
Expert's answer
2021-09-03T00:10:11-0400
#include<stdio.h>
#include<stdlib.h>
struct Node{
	char data;
	struct Node * nextNode;
};


struct Node *insert(struct Node *start, char item){
	struct Node *tempNode;
	 tempNode= (struct Node *)malloc(sizeof(struct Node));
	
	tempNode->data = item;
	tempNode->nextNode = start;
	start = tempNode;
	return start;
	
}
	
void display(struct Node * head){
	struct Node *temp;
	temp = head;
	if(head==NULL){
		printf("List is empty\n");
	}
	while(temp !=NULL){
		
		printf("%c\t", temp->data);
		temp = temp->nextNode;
	}
}
void search(struct Node *head, char item){
	struct Node * temp = head;
	int pos = 1;
	while(temp != NULL){
		if(temp->data==item){
			printf("\nItem %c found at position %d\n",item,pos);
		}
		temp=temp->nextNode;
		pos++;
	}
	
	
}
struct Node *dele(struct Node *start){
	struct Node * temp;
	if(start == NULL)
{
printf("Underflow"); 
}
else
{
temp= start;
start = start -> nextNode;
free(temp);
}
return start;
}
int main(){
	struct Node *node= NULL;
	node = insert(node,'a');
	node = insert(node,'b');
	node = insert(node,'c');
	node = insert(node,'d');
	printf("The linked list is:\n");
	display(node);
	node = dele(node);
	printf("\nThe linked list after delete operation at the beginning is:\n");
	display(node);
	//Searching a in the linked list
	search(node,'a');
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS