Skip to content

Instantly share code, notes, and snippets.

@SanowerTamjit
Created March 21, 2019 20:06
Show Gist options
  • Save SanowerTamjit/3a6e26050bf453451f4e6704a4cef730 to your computer and use it in GitHub Desktop.
Save SanowerTamjit/3a6e26050bf453451f4e6704a4cef730 to your computer and use it in GitHub Desktop.
#define NULL_VALUE -99999
#define SUCCESS_VALUE 99999
struct listNode
{
int item ; //will be used to store value
struct node *next ; //will keep address of next node
struct node *prev ; //will keep address of previous node
} ;
struct listNode * list ;
struct listNode * tail ;
void initializeList()
{
printf("\n--------->Initializing<---------\n");
list = 0 ; //set to NULL
tail = 0 ; //set to NULL
}
int insertAtFirst(int data)
{
struct listNode *newNode, *head;
newNode = (struct listNode*)malloc(sizeof(struct listNode));
newNode->item = data; //Insert data on new item
newNode->next = list; //point next node by list value;
newNode->prev = 0; //no previous node at first;
list = newNode;
head = newNode->next;
if(head != 0){
head->prev = newNode;
}
else
tail = newNode; //first inserted node act as tail;
return SUCCESS_VALUE ;
}
int insertAtLast(int data)
{
struct listNode *newNode, *head;
newNode = (struct listNode*)malloc(sizeof(struct listNode));
newNode->item = data; //Insert data on new item
newNode->prev = tail; //point prev node as tail
newNode->next = 0; //no previous node at last;
tail = newNode;
head = newNode->prev;
if(head != 0){
head->next = newNode;
}
else
list = newNode; //first inserted node act as listHead;
return SUCCESS_VALUE ;
}
// Delete or remove the first node
void deleteFromFirst()
{
struct node * toDelete;
if(list == 0)
{
printf("Unable to delete. List is empty.\n");
}
else
{
toDelete = list;
list = list->next; // Move head pointer to 2 node
if (list != 0)
list->prev = 0; // Remove the link to previous node
free(toDelete); // Delete the first node from memory
printf("\nDeleted\n");
}
}
// Delete or remove the last node
void deleteFromLast()
{
struct node * toDelete;
if(tail == 0)
{
printf("Unable to delete. List is empty.\n");
}
else
{
toDelete = tail;
tail = tail->prev; // Move head pointer to 2 node
if (tail != 0)
tail->next = 0; // Remove the link to previous node
free(toDelete); // Delete the first node from memory
printf("\nDeleted\n");
}
}
//Delete from position;
void deleteFromPos(int position)
{
struct listNode *current, *tempNode;
int i;
current = list;
for(i=1; i<position && current!=0; i++)
{
current = current->next;
}
if(position == 1)
deleteFromFirst();
else if(position < 1)
printf("Invalid position!\n");
else if(current == tail)
deleteFromLast();
else if(current != 0)
{
tempNode = current->prev;
tempNode->next = current->next;
tempNode = current->next;
tempNode->prev = current->prev;
free(current); // Delete the n node
printf("\nDeleted\n");
}
else
printf("Invalid position!\n");
}
// Show all node values
void showDoublyList()
{
struct listNode * tmp, *tempNode;
int n = 1;
if(list == 0)
{
printf(" No data found in the List.");
}
else
{
tmp = list;
printf("\n\n List Data are :\n");
while(tmp != 0)
{
printf(" node %d : %d\t", n, tmp->item);
tempNode = tmp->prev;
if(tempNode != 0)
printf("(prev: %d)",tempNode->item);
else
printf("(prev: 0)");
tempNode = tmp->next;
if(tempNode != 0)
printf("(next: %d)",tempNode->item);
else
printf("(next: 0)");
printf("\n");
n++;
tmp = tempNode;
}
}
}
// Show all node values reverst
void showDoublyListReverse()
{
struct listNode * tmp, *tempNode;
printf("\n\nReverse Print");
int n = 1;
if(tail == 0)
{
printf(" No data found in the List.");
}
else
{
tmp = tail;
printf("\n\n List Data are :\n");
while(tmp != 0)
{
printf(" node %d : %d\t", n, tmp->item);
tempNode = tmp->next;
if(tempNode != 0)
printf("(next: %d)",tempNode->item);
else
printf("(next: 0)");
tempNode = tmp->prev;
if(tempNode != 0)
printf("(prev: %d)",tempNode->item);
else
printf("(prev: 0)");
printf("\n");
n++;
tmp = tempNode;
}
}
}
int main()
{
initializeList();
showDoublyList();
insertAtLast(2);
insertAtLast(4);
insertAtLast(5);
insertAtLast(6);
showDoublyList();
initializeList();
insertAtFirst(1);
insertAtFirst(41);
insertAtFirst(12);
insertAtFirst(6);
showDoublyList();
//deleteFromFirst();
//showDoublyList();
//deleteFromLast();
//showDoublyList();
deleteFromPos(0);
showDoublyList();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment