Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 10
- struct node{
- int info,next;
- }n[SIZE];
- int avail = 0;
- int assignNodeNext()
- {
- int i;
- for(i=0; i<(SIZE - 1); i++)
- n[i].next = i+1;
- n[SIZE - 1].next = -1;
- }
- int getNode()
- {
- int p;
- if (avail == -1)
- {
- printf("Error: Overflow.\n");
- return avail;
- }
- p = avail;
- printf("Enter the value: ");
- scanf("%d", &n[p]);
- avail = n[avail].next;
- return p;
- }
- int freeNode(int p)
- {
- n[p].next = avail;
- avail = p;
- n[p].info = 0;
- return 0;
- }
- void display()
- {
- int i;
- for(i = 0; i<SIZE; i++)
- printf("| %d ",n[i]);
- printf("|");
- }
- void choice()
- {
- int choice = 0;
- int p;
- while (choice != -1)
- {
- printf("\n1. Insert. 2. Delete 3. display: ");
- scanf("%d",&choice);
- if (choice == 1)
- getNode();
- if (choice == 2)
- {
- printf("Delete which node?: ");
- up: scanf("%d", &p);
- if(p>-1 && p<SIZE)
- freeNode(p);
- else
- {
- printf("\nWrong input. Try again.");
- goto up;
- }
- }
- if (choice == 3)
- display();
- }
- }
- int main()
- {
- assignNodeNext();
- choice();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement