Προς το περιεχόμενο

υλοποιηση σωρου σε C


korgoroth

Προτεινόμενες αναρτήσεις

Δημοσ.

καλησπερα αυπνοι φιλοι.

 

εχω ενα θεμα με μια εργασια για διαχειριση σωρου

Συγεκριμενα : εισαγωγή στοιχείου, μέγιστο στοιχείο, εξαγωγή μέγιστου στοιχείου, δημιουργία

σωρού με αυτες τις συναρτησεις, καποια συμβουλη για το που τηα ωρς ενα καλο tutorial???

Δημοσ.

To καλύτερο tutorial είναι να πας στην βιβλιοθήκη και να πάρεις ένα βιβλίο για δομές δεδομένων.

Να μια αρχή από μένα :

 

 

>
/*

stackll.c

demo implementation of a Stack data structure using a Linked List

     -------------
      V.I.Smirnov
     -------------
*/

#include<stdio.h>
#include<stdlib.h>
#define size 10

struct stack
{ 
 int data;
 struct stack *next; // self referential structure
};


typedef struct stack stk;
stk *tos=NULL;

void push();
int pop();
void display();


void main()
{ int choice=0;
 int val;

do
{ 
  printf("\n Menu Details........ \n");
  printf("\n 1. push a data item onto stack and display");
  printf("\n 2. pop a data item from stack and display");
  printf("\n 3. display stack");
  printf("\n 4. exit");
  printf("\n enter your choice:");
  scanf("%d",&choice);

switch(choice)
{
  case 1:
     push();
     display();
     break;

  case 2: 
     val=pop();
     printf("\n pop up value= %d",val);
     printf("\n stack after pop up");
     display();
     break;

  case 3: 
     display();
     break;

  case 4: 
     printf("exiting from the programm");
     break;

 default: 
     printf("\nwrong choice<enter between 1 & 4 only");
}

}while(choice!=4);

}


void push()
{ 
stk *node; // node is new node to be pushed as first node
node=(stk*)malloc(sizeof(stk));
printf("\n enter data to be pushed on to stack:");
scanf("%d",&node->data);

//make node as first node of stack

node->next=tos;
tos=node; // now tos points to new node inserted
}

int pop()
{ 
int val; // val returns data item from top of stack
// store first node in temp
stk *temp;
temp=tos;

//check if stack is empty
if (tos==NULL)
{ printf("\n stack is empty");
 exit(0);
}
else
{  val=tos->data;
  
  // shift tos to next element
  tos=tos->next;
  free(temp); // release temp
}
return val;
}


void display()
{ 
  stk *temp;
  temp=tos; // we will use temp for traversing the stack
  printf("\n Stack elements are....\n");

  if (temp == NULL)
     printf("\n stack is empty");
  else
  { 
     while (temp->next!=NULL)
     { printf(" %d \n",temp->data);
       temp=temp->next;
     } 
  //now last element still left for display
  printf("%d",temp->data);
  }
}

Λείπουν τα σχετικά με το μέγιστο στοιχείο. Πρόσθεσέ τα στο παραπάνω demo και τέλειωσες...

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...