Skip to content
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
91 changes: 91 additions & 0 deletions CODE in C/Data Structures/Delete_node_without_head.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include<stdio.h>
#include<stdlib.h>
struct linked{
int data;
struct linked *next;
};
typedef struct linked l;
l *start ,*node,*temp;
void traversal()
{ l *s=start;
printf("\nList is: ");
while(s!=NULL)
{
printf("%d ",s->data);
s=s->next;

}
printf("\n");
}
void insert_begn(int newdata){
node=(l*)malloc(sizeof(l));
node->data=newdata;
node->next=start;
start=node;
}
void insert_end(int newdata)
{
l *p=start;
node=(l*)malloc(sizeof(l));
node->data=newdata;
node->next=NULL;
while(p->next!=NULL)
p=p->next;
p->next=node;
}

void deleteNodeWithoutHead(l *pos)
{
if (pos == NULL) // If linked list is empty
return;
else {

if (pos->next == NULL) {
printf("This is last node, require head, can't be freed\n");
return;
}

temp = pos->next;

// Copy data of the next node to current node
pos->data = pos->next->data;

// Perform deletion
pos->next = pos->next->next;

free(temp);
}
}

int main(){
int k,r;
start=NULL;
printf("options:\n1 for traversal\n2 for insert at begin\n3 for insert at end\n4 To perform deletion without using head pointer\n");
printf("_______________\n");
int op;
do{scanf("%d",&op);
switch(op)
{
case 1:
traversal();
break;
case 2:
scanf("%d",&k);
insert_begn(k);
traversal();
break;
case 3:
scanf("%d",&k);
insert_end(k);
traversal();
break;

}
}while(op!=4);
//now delete second element without heador pass the node which u want to delete
l *del=start->next;
deleteNodeWithoutHead(del);
traversal();
return 0;
}

86 changes: 86 additions & 0 deletions CODE in C/Data Structures/Reverse_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//C program to Reverse String using STACK

#include <stdio.h>
#include <string.h>

#define MAX 1000

int top=-1;
int item;

char stack_string[MAX];

void pushChar(char item);

//function to pop character (item)
char popChar(void);

//function to check stack is empty or not
int isEmpty(void);

//function to check stack is full or not
int isFull(void);

int main()
{
char str[MAX];

int i;

printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces

for(i=0;i<strlen(str);i++)
pushChar(str[i]);

for(i=0;i<strlen(str);i++)
str[i]=popChar();

printf("Reversed String : %s\n",str);

return 0;
}

void pushChar(char item)
{
//check for full
if(isFull())
{
printf("\nStack is FULL !!!\n");
return;
}

top=top+1;
stack_string[top]=item;
}

char popChar()
{
if(isEmpty())
{
printf("\nStack is EMPTY!!!\n");
return 0;
}

//pop item and decrease top
item = stack_string[top];
top=top-1;
return item;
}

int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}

int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;
}