0% found this document useful (0 votes)
6 views

C Linked List - Programming Exercises

Uploaded by

Aravind AR7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Linked List - Programming Exercises

Uploaded by

Aravind AR7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Singly linked lists

========================
#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node *next;
};

int main(){
int choice,elem;
struct node *first=NULL,*temp,*current,*prev;

while(1){
printf("\n1: Insert\n2: Delete\n3: Display\n4: Exit\n");
printf("Enter the choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Value:");
scanf("%d",&elem);
temp=(struct node*)malloc(sizeof(struct node));
temp->value=elem;
temp->next=NULL;

if(first==NULL)
first=temp;
else{
current=first;
while(current->next!=NULL){
current=current->next;
}
current->next=temp;
}
break;
case 2:
if(first==NULL)
printf("List Empty");
else{
printf("\nEnter the element to be deleted: ");
scanf("%d",&elem);
if(first->value==elem)
first=first->next;
else{
current=first;
prev=first;
while(current->value!=elem && current != NULL){
prev=current;
current=current->next;
}
if(current == NULL)
printf("Element not found!!!");
else{
prev->next=current->next;
printf("\nDeleted...");
}
}
}
break;
case 3: printf("\nThe list is ");
if(first==NULL)
printf("Empty\n");
else{
current=first;
while(current!=NULL){
printf(" %d ",current->value);
current=current->next;
}
printf("\n");
break;
case 4: exit(0);
default: printf("\nInvalid choice!!!\n");
}

}
}
}
============================
Queue using arrays
#include<stdio.h>
#include<stdlib.h>
#define Max_size 10
int main(){
int queue[10];
int front=-1,rear=-1;
int val,i,choice;

while(1){
printf("\n1: Enqueue\n2: Dequeue\n3: Display\n4: Exit\n");
printf("Enter the choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be enqueued: ");
scanf("%d",&val);
if(rear==Max_size)
printf("Queue overflow!!");
else{
rear++;
queue[rear]=val;

if(front==-1){
front++;
}
}
break;
case 2: if(front==-1)
printf("Queue Underflow!!");
else{
printf("\n%d dequeued..",queue[front]);
front++;
if(front>rear)
front=rear=-1;
}

break;
case 3: printf("\nThe queue is: ");
if(front==-1)
printf("Empty!!!\n");
else{
for(i=front;i<=rear;i++)
printf(" %d ",queue[i]);

}
break;
case 4: exit(0);
default: printf("\nInvalid choice!!!\n");
}

}
}
============================
//Stack using linked lists

#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node *next;
};

int main(){
int choice,elem;
struct node *top=NULL,*temp;
while(1){
printf("\n1: Push\n2: Pop\n3: Display\n4: Exit\n");
printf("Enter the choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Value:");
scanf("%d",&elem);
temp=(struct node*)malloc(sizeof(struct node));
temp->value=elem;
temp->next=NULL;

if(top==NULL)
top=temp;
else{
temp->next=top;
top=temp;
}
break;
case 2:
if(top==NULL)
printf("Stack Empty");
else{
printf("\n%d popped\n",top->value);
top=top->next;
}
break;
case 3: printf("\nThe stack is ");
if(top==NULL)
printf("Empty\n");
else{
temp=top;
while(temp!=NULL){
printf(" \n%d ",temp->value);
temp=temp->next;
}
printf("\n");
break;
case 4: exit(0);
default: printf("\nInvalid choice!!!\n");
}

}
}
}
==============================
Stack using arrays
#include<stdio.h>
#include<stdlib.h>
#define Max_size 10
int main(){
int stack[10];
int top=-1;
int val,i,choice;

while(1){
printf("\n1: Push\n2: Pop\n3: Display\n4: Exit\n");
printf("Enter the choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be pushed: ");
scanf("%d",&val);
if(top==Max_size-1)
printf("Stack overflow!!");
else{
top++;
stack[top]=val;
}
break;
case 2: if(top==-1)
printf("Stack empty!!");
else{
printf("\n%d popped..",stack[top]);
top--;

}
break;
case 3: printf("\nThe stack is: ");
if(top==-1)
printf("Empty!!!\n");
else{
for(i=top;i>=0;i--)
printf(" \n%d ",stack[i]);

}
break;
case 4: exit(0);
default: printf("\nInvalid choice!!!\n");
}

}
}

You might also like