Created
May 20, 2017 18:25
-
-
Save muhammedeminoglu/6aec7dec01b01043168460b139b19e0e to your computer and use it in GitHub Desktop.
All the Stack Operations with Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
struct person{ | |
char firstName[19]; | |
char secondName[19]; | |
int age; | |
struct person *next; | |
}; | |
struct person* top = NULL; | |
struct person* temp; | |
char personName[19]; | |
char personSecondName[19]; | |
int personAge; | |
struct person* createPerson(char name[19], char sirName[19], int age) | |
{ | |
struct person* newPerson = (struct person*)malloc(sizeof(struct person)); | |
strcpy(newPerson->firstName, name); | |
strcpy(newPerson->secondName, sirName); | |
newPerson->age = age; | |
newPerson->next = top; | |
return newPerson; | |
} | |
void pushNode(char name[], char sirName[], int age) | |
{ | |
struct person* newFirst = createPerson(name, sirName, age); | |
newFirst->next = top; | |
top = newFirst; | |
} | |
void popNode() | |
{ | |
if(top == NULL) | |
{ | |
printf("\n*************************************\n"); | |
printf("\n You Can't pop, Your Stack is EMPTY!"); | |
} | |
else | |
{ | |
temp = top->next; | |
free(top); | |
top = temp; | |
} | |
} | |
void printList() | |
{ | |
int i = 1; | |
if(top == NULL) | |
{ | |
printf("\n*************************************\n"); | |
printf("\n Your list is empty ..."); | |
} | |
else | |
{ | |
temp = top; | |
printf("\n*************************************\n"); | |
while(temp->next != NULL) | |
{ | |
printf("%d-) %s %s %d\n", i, temp->firstName, temp->secondName, temp->age); | |
temp = temp->next; | |
i++; | |
} | |
printf("%d-) %s %s %d\n", i, temp->firstName, temp->secondName, temp->age); | |
} | |
} | |
struct person* peek() | |
{ | |
if(top == NULL) | |
{ | |
printf("\n*************************************\n"); | |
printf("\n Your list is empty ..."); | |
return 0; | |
} | |
else | |
{ | |
return top; | |
} | |
} | |
int main() | |
{ | |
int choise; | |
struct person* topElement; | |
while(1 == 1) | |
{ | |
printf("\n1- Add Person to Stack ..."); | |
printf("\n2- Pop Person from Stack ..."); | |
printf("\n3- Peek Top Node..."); | |
printf("\nSelect Your Choise ... "); | |
scanf("%d", &choise); | |
switch(choise) | |
{ | |
case 1: | |
printf("\n First Name ? "); | |
scanf("%s", &personName); | |
printf("\n Second Name ? "); | |
scanf("%s", &personSecondName); | |
printf("\n Age ? "); | |
scanf("%d", &personAge); | |
pushNode(personName, personSecondName, personAge); | |
printList(); | |
break; | |
case 2: | |
popNode(); | |
printList(); | |
break; | |
case 3: | |
topElement= peek(); | |
if(topElement) | |
printf("\n Your TOP ELEMENT is ==> %s %s %d", topElement->firstName, topElement->secondName, topElement->age); | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment