1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <stdio.h>
#include <stdlib.h>
struct list {
int x;
struct list *next, *prev, *level, *rend;
} *base;
void newin(), deleteone(), listhem();
int main (void)
{
char ch;
base = (struct list *) NULL;
while (1) {
printf ("\n\nType 'E/e' to enter a new number, use - number to quit");
printf ("\n 'D/d' to delete a number from the list");
printf ("\n 'L/l' to list all numbers");
printf ("\n 'Q/q' to quit : ");
scanf ("%s", &ch);
switch (ch) {
case 'E' :
case 'e' : newin ();
break;
case 'D' :
case 'd' : deleteone();
break;
case 'L' :
case 'l' : listhem();
break;
case 'Q' :
case 'q' : printf ("\nEnter only selections listen above!\n");
}
}
return 0;
}
void newIn ()
{
int pos_input = 1;
struct list *newIn, *p;
newIn = (struct list *) malloc (sizeof (struct list));
while (pos_input) {
printf ("\nEnter new number: ");
scanf ("%d", &(newIn->x));
if (newIn ->x > 0) {
if (base == (struct list *) NULL) {
newIn -> next = base;
base = newIn;
}
else {
p = base;
if (newIn -> x >= p -> x) {
newIn -> next = base;
base = newIn;
}
else
while (p->next->x >= new->x)
p = p->next;
new->next = p->next;
p->next = new;
}
}
new = (struct list *) malloc (sizeof (struct list));
}
else
pos_input = 0;
}
}
void deleteone()
{
struct list *p;
int num;
p= base;
printf ("\nEnter num to delete : ")
scanf ("%d", &num);
if (base == NULL)
printf ("\nNo number in the list!");
else
if (p->x == num) {
base = p->next;
printf ("\n%d is deleted from the list.", num);
}
else {
while (p->next->x != num && p != NULL)
p = p->next;
if (p->next == NULL)
printf ("\n%d is not in the list, try next!", num);
else {
p->next = p->next->next;
printf ("\n%d is deleted from the list.", num);
}
}
}
void listhem ()
{
struct list *this, *p;
if (base == (struct list *) NULL) {
printf ("\n\nEmpty list.\n");
return;
}
else {
printf ("\nThe following numbers are in backward (D) order :\n");
this = base;
do {
printf ("%d, ", this->x);
this = this->next;
} while (this != (struct list *) NULL);
printf ("\nThe following numbers are in foreward (A) order :\n");
p = (struct list *) NULL;
do
this = base;
for (; this->next != p; this = this->next);
printf ("%d, " this->x);
p = this;
} while (p != base);
}
}
|