#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');
}
Comments
Leave a comment