Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Created May 20, 2017 18:23
Show Gist options
  • Save muhammedeminoglu/90637c27736e4de67f1bf19d6eb7448c to your computer and use it in GitHub Desktop.
Save muhammedeminoglu/90637c27736e4de67f1bf19d6eb7448c to your computer and use it in GitHub Desktop.
Singly Linked List All Operation
#include <stdio.h>
#include <stdlib.h>
////// ****** AUTHOR Muhammed Eminoðlu ****** \\\\\
//////*****************************************\\\\\
//////******************************************\\\\\
//////**********www.muhammedeminoglu.com*********\\\\\
//////**********www.algoritmauzmani.com***********\\\\\
//Global variables
int choise;
struct node{
int data;
struct node *next;
};
struct node* start = NULL;
struct node* temp;
struct node* createNode()
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));
return newNode;
}
void addLast(int x)
{
struct node* element = createNode();
element->data = x;
element->next = NULL;
if(start == NULL)
{
start = element;
}
else
{
temp = start;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = element;
}
}
void addFirst(int y)
{
struct node* element = createNode();
element->data = y;
temp = start;
element->next = temp;
start = element;
}
void addPos(int z, int index)
{
int counter = 0;
struct node* anywhere = createNode();
anywhere->data = z;
temp = start;
if(index == 0)
{
addFirst(z);
}
else
{
while(temp != NULL)
{
if(counter+1 == index)
break;
temp = temp->next;
counter++;
}
struct node* temp1;
temp1 = temp->next;
temp->next = anywhere;
anywhere->next = temp1;
}
}
void deleteLast()
{
if(start == NULL)
{
printf("\n Your List is EMPTY");
}
else
{
temp = start;
if(start->next == NULL)
{
free(start);
start = NULL;
}
else
{
while(temp->next->next != NULL)
{
temp = temp->next;
}
struct node* cukubik = temp->next;
free(cukubik);
temp->next = NULL;
}
}
}
void deleteFirst()
{
if(start != NULL)
{
if(start->next != NULL)
{
struct node* temp2 = start;
start = start->next;
free(temp2);
}
else
{
free(start);
start = NULL;
}
}
else
{
printf("\nThere is no item to be deleted, Please add item...");
}
}
void deleteSpes(int t)
{
if(start->data == t)
{
deleteFirst();
}
else
{
temp = start;
while(temp->next->data != t)
{
temp = temp->next;
}
struct node*temp1 = temp->next->next;
struct node* temp2 = temp;
free(temp->next);
temp2->next = temp1;
}
}
void reverseLinked()
{
struct node* prevNode = NULL;
struct node* nextNode;
temp = start;
while(temp != NULL)
{
nextNode = temp->next;
temp->next = prevNode;
prevNode = temp;
temp = nextNode;
}
start = prevNode;
}
void printLinked()
{
if(start == NULL)
{
printf("\n Your List is EMPTY");
}
else
{
temp = start;
printf("\n********************\n");
while(temp->next != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("%d ", temp->data);
printf("\n");
}
}
void menu()
{
while(1 == 1)
{
printf("\n1- Insert an item to last position ... ");
printf("\n2- Insert an item first position ... ");
printf("\n3- Insert an item any position ... ");
printf("\n4- Delete an item from last position ... ");
printf("\n5- Delete an item from first position ... ");
printf("\n6- Delete an item from any position ... ");
printf("\n7- Reverse Linked List ... ");
printf("\n PLEASE MAKE YOUR CHOISE ... ");
scanf("%d", &choise);
selection(choise);
}
}
void selection(int choise)
{
int item, i, index;
switch(choise)
{
case 1:
printf("\n How many item will you add? ... ");
scanf("%d", &item);
for(i = 0; i < item; i++)
{
printf("\n Please add %d. number... ", i+1);
scanf("%d", &choise);
addLast(choise);
printLinked();
}
break;
case 2:
printf("Which element(number) do you want to add first position? ... ");
scanf("%d", &item);
addFirst(item);
printLinked();
break;
case 3:
printf("Which element do you want to add? ...");
scanf("%d", &item);
printf("Which index? (it will be added on index+1 ");
scanf("%d", &index);
addPos(item, index);
printLinked();
break;
case 4:
deleteLast();
printLinked();
break;
case 5:
deleteFirst();
printLinked();
break;
case 6:
printf("\n Which number do you want to delete? ...");
scanf("%d", &item);
deleteSpes(item);
printLinked();
break;
case 7:
reverseLinked();
printLinked();
}
}
int main()
{
menu();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment