Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /*Name: Hossam El-Din Haybat Mohamed */
- /*ID:2208*/
- typedef struct Node{
- int Value;
- struct Node *Next;
- struct Node *Previous;
- }Node;
- Node *Head=NULL;
- Node *Tail=NULL;
- Node *Current_Value;
- void Test () {
- Node *Display;
- Display = Head;
- printf("\nThe printed full queue:\n");
- printf("=======================\n\n");
- while(Display) {
- printf("[%d]->",Display->Value);
- Display=Display->Next;
- }
- printf("[NULL]\n\n");
- }
- void Insert_First(int X);
- void Count ();
- void Insert_Last(int X);
- void Remove_First ();
- void Remove_Last ();
- void Last();
- void First();
- void Is_Empty();
- int main(){
- int X;
- int Character;
- printf("\nChoose selection:\n");
- printf("=================\n\n");
- printf("[1] Insert first.\n");
- printf("[2] Insert last.\n");
- printf("[3] Remove first.\n");
- printf("[4] Remove last.\n");
- printf("[5] First.\n");
- printf("[6] Last.\n");
- printf("[7] Is empty?\n");
- printf("[8] Size?\n");
- scanf("%d",&Character);
- if(Character==1) {
- printf("Enter variable:");
- scanf("%d",&X);
- Insert_First(X);
- }
- if(Character==2) {
- printf("Enter variable:");
- scanf("%d",&X);
- Insert_Last(X);
- }
- if(Character==3) {
- Remove_First();
- }
- if(Character==4) {
- Remove_Last();
- }
- if(Character==5) {
- First();
- }
- if(Character==6) {
- Last();
- }
- if(Character==7) {
- Is_Empty();
- }
- if(Character==8) {
- Count();
- }
- else {
- system("cls");
- main();
- }
- return 0;
- }
- void Insert_First(int X) {
- system("cls");
- Current_Value = (Node*)malloc(sizeof(Node));
- if(Tail==NULL && Head==NULL) {/*Queue is empty*/
- Tail=Current_Value;
- Head=Current_Value;
- Current_Value->Value=X;
- Current_Value->Next=NULL;
- Current_Value->Previous=NULL;
- Test();
- main();
- }
- Current_Value->Value=X;
- Current_Value->Next=Head;
- Head->Previous=Current_Value;
- Head=Current_Value;
- Head->Previous=NULL;
- Test();
- main();
- }
- void Insert_Last(int X) {
- system("cls");
- Current_Value = (Node*)malloc(sizeof(Node));
- if(Tail==NULL && Head==NULL) {/*Queue is empty*/
- Tail=Current_Value;
- Head=Current_Value;
- Current_Value->Value=X;
- Current_Value->Next=NULL;
- Current_Value->Previous=NULL;
- Test();
- main();
- }
- Current_Value->Value=X;
- Current_Value->Previous=Tail;
- Tail->Next=Current_Value;
- Tail=Current_Value;
- Tail->Next=NULL;
- Test();
- main();
- }
- void Remove_First () {
- system("cls");
- if(Head==NULL) {/*Queue is empty*/
- printf("\nERROR! Queue is empty!\n\n");
- main();
- }
- Node *Dequeue;
- Dequeue=Head;
- Head=Head->Next;
- if(Head==NULL) {
- Tail=NULL;
- Dequeue->Next=NULL;
- printf("Dequeued Number:[%d]\n\n",Dequeue->Value);
- free(Dequeue);
- Test();
- main();
- }
- Head->Previous=NULL;
- Dequeue->Next=NULL;
- printf("Dequed Number:[%d]\n\n",Dequeue->Value);
- free(Dequeue);
- Test();
- main();
- }
- void Remove_Last () {
- system("cls");
- if(Tail==NULL) {/*Queue is empty*/
- printf("\nERROR! Queue is empty!\n\n");
- main();
- }
- Node *Dequeue;
- Dequeue=Tail;
- Tail=Tail->Previous;
- if(Tail==NULL) {
- Head=NULL;
- Dequeue->Previous=NULL;
- printf("Dequeued Number:[%d]\n\n",Dequeue->Value);
- free(Dequeue);
- Test();
- main();
- }
- Tail->Next=NULL;
- Dequeue->Previous=NULL;
- printf("Dequeued Number:[%d]\n",Dequeue->Value);
- free(Dequeue);
- Test();
- main();
- }
- void First () {
- system("cls");
- printf("First element is:[%d].",Head->Value);
- main();
- }
- void Last() {
- system("cls");
- printf("Last element is:[%d].",Tail->Value);
- main();
- }
- void Is_Empty () {
- system("cls");
- if(Head==NULL) {
- printf("Queue is empty.");
- } else {
- printf("Queue is not empty.");
- }
- main();
- }
- void Count () {
- system("cls");
- Node *Display;
- int Counter=0;
- Display=Head;
- while(Display) {
- Counter++;
- Display=Display->Next;
- }
- printf("Number of elements: [%d].",Counter);
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment