Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions c/stack_using_linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>


typedef struct Stack
{
int info;
struct Stack *next;
}Stack;

void push(Stack * s, int data)
{
/*
* Return value : void
* Input : Pointer to the stack "s",Integer to be inserted "data"
*/
Stack *temp=malloc(sizeof(Stack));
temp->info=data;
temp->next=NULL;

if(s->next!=NULL)
{
temp->next=s->next;
}
s->next=temp;
}

void pop(Stack * s)
{
/*
* Return value : void
* Input : Pointer to the stack "s"
*/
if(s->next==NULL)
{
printf("Empty\n");
}
else
{
Stack *temp;
temp=s->next;
s->next=temp->next;
free(temp);
temp->next=NULL;
}
}

void display(Stack * s)
{
/*
* Return value : void
* Input : Pointer to the stack "s"
*/
if(s->next==NULL)
{
printf("Empty\n");
return;
}
else
{
Stack * temp;
temp=s->next;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
printf("\n");
}
}

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Stack * s = (Stack *)malloc(sizeof(Stack));
while(1)
{
int choice;
printf("\n\n1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Exit Program\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int data;
printf("Enter the data to be pushed\n");
scanf("%d",&data);
push(s,data);
}
break;

case 2:
{
pop(s);
}
break;

case 3:
{
display(s);
}
break;

case 4:
{
return 0;
}
break;
}
}
return 0;
}
222 changes: 222 additions & 0 deletions operation_on_Linked_List.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

typedef struct node{
int val;
struct node *next;
}node;


void display(node* list){
//Print space separated integers from the linked list
//list is the pointer pointing to the beginning of the linked list
node* ptr=list;
if(ptr==NULL)
{
printf("Empty\n");
}
while(ptr!=NULL)
{
printf("%d ",ptr->val);
ptr=ptr->next;
}
printf("\n");
}

node* insertAtBeginning(node* list,int val){
//Write function to insert val at the beginning of the list and return a pointer pointing to beginning of the list
node *ptr=(node *) malloc(sizeof(node));
ptr->val=val;
if(list==NULL)
{
list=ptr;
}
else
{
ptr->next=list;
list=ptr;
}
return list;
}

node* insertAtEnd(node* list,int val){
//Write function to insert val at the end of the list and return a pointer pointing to beginning of the list
node *n=(node *) malloc(sizeof(node));
n->val=val;
if(list==NULL)
{
list=n;
}
else
{
node *ptr=list;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=n;
}
return list;
}

node* insertAtPosition(node* list,int val,int pos){
//Write function to insert "val" at "pos" in the list and return a pointer pointing to beginning of the list
node *temp=(node*) malloc(sizeof(node));
int i,count=0;
temp->val=val;
if(pos==0)
{
temp->next=list;
list=temp;
}
i=0;
node *ptr=list;
while(i<pos-1)
{
ptr=ptr->next;
i++;
}
temp->next =ptr->next ;
ptr->next=temp;
return list;
}

node* deleteFromBeginning(node* list,int* flag){
//Delete from the beginning of the list and return pointer pointing to the beginning of the list
//If deletion not possible set flag as -1 and return pointer to beginning of list
if(list==NULL)
{
*flag=-1;
}
else
{
node *ptr=list;
list=list->next;
free(ptr);
ptr=NULL;
}
return list;
}

node* deleteFromEnd(node* list,int* flag){
//Delete from the end of the list and return pointer to the beginning of the list
//If deletion not possible set flag as -1 and return pointer to beginning of list

node *temp=( node*) malloc(sizeof(node));
if(list==NULL)
{
*flag=-1;
}
else if(list->next==NULL)
{
free(list);
list=NULL;
}
else
{
node *ptr=list;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}

temp->next=NULL;
free(ptr);
ptr=NULL;
}
return list;
}

node* deleteFromPosition(node* list,int pos,int* flag){
//Delete from the "pos" of the list and return pointer to the beginning of the list
//If deletion not possible set flag as -1 and return pointer to beginning of list

int i,notfound=1;
if(list==NULL)
{
*flag=-1;
}
else if(pos==0)
{
node *ptr=list;
list=ptr->next;
free(ptr);
}
else
{
node *ptr=list;
node *temp=list;
for(i=0;i<pos && ptr->next!=NULL;i++)
{
temp=ptr;
ptr=ptr->next;
}
if(ptr->next==NULL)
{
*flag=-1;
}
else
{
temp->next=ptr->next;
free(ptr);
ptr=NULL;
}
}

return list;

}


int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
node* first = NULL;
int opt,pos=1,val;
while(opt != 8){
printf("\n1.Display");
printf("\n2.Insert in the Begining");
printf("\n3.Insert at the End");
printf("\n4.Insert at the specific position");
printf("\n5.Delete from Begining");
printf("\n6.Delete fron End");
printf("\n7.Delete from specific position");
printf("\n8.Exit program\n");
scanf("%d",&opt);
int flag = 0;
switch(opt){
case 1: display(first);
break;
case 2: scanf("%d",&val);//printf("insBe %d\n",val);
first = insertAtBeginning(first,val);
break;
case 3: scanf("%d",&val);//printf("insEn %d\n",val);
first = insertAtEnd(first,val);
break;
case 4: scanf("%d %d",&val,&pos);// printf("insPos %d pos %d\n",val,pos);
first = insertAtPosition(first,val,pos);
break;
case 5: first = deleteFromBeginning(first,&flag);//printf("delBe\n");
if(flag == -1)
printf("Deletion not possible\n");
break;
case 6: first = deleteFromEnd(first,&flag); //printf("delEnd\n");
if(flag == -1)
printf("Deletion not possible\n");
break;
case 7: scanf("%d",&pos);//printf("delPos %d\n",pos);
first = deleteFromPosition(first,pos,&flag);
if(flag == -1)
printf("Deletion not possible\n");
break;
}
}


return 0;
}