task of C + +

Here's what the task:
1.Establishment a list arranged in ascending order elements (introduced in random order) and display the list screen.
2.Delete of a user-specified element.

I wrote that:

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
#include<iostream.h>

struct elem
{int key ; elem*next;} *start = NULL;

void add(int n)
{
	elem*p = start;
	start = new elem;
	start ->key = n;
	start ->next = p;
}

int del(int n)
{
	elem*q,*p=start;
	if(start)
	{
		while(p->key!=n)
		{q=p;p=p->next;}
		n=p->key;
		q->next=p->next;
		delete p;
		return 1;
	}
	else
		return 0;
}
void list()
{
	elem*p = start;
	while(p)
	{
		cout<<p->key<<' ';
		p = p->next;
	}
}
void main()
{
	int k, num, n;
	do
	{
		cout<<"     MENU\n";
		cout<<"1 - Insert element\n";
	        cout<<"2 - Delete any element\n";
		cout<<"3 - Exit\n";
		cout<<"\nEnter your choice:";
		cin>>n;
		if(n==1)
		{
			cout<<"\nInput num:";
			cin>>num;
		}
        
		if(n==2)
		{
			cout<<"\nEnter number for deleting:";
				cin>>num;
		}
     switch(n)
	 {case(1):{add(num);list();break;}
	  case(2):{del(num);cout<<"\nDeleted element:"<<num<<"\n";list();break;}
	 }
	}
	while(n!=3);
}



I have a problem with the arrangement of the elements in ascending order and deleting

Last edited on
The code should probably be something along the lines of (by which I mean, "take this with a bagful of salt, I haven't tested it." There may especially be some exceptions I'm missing)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void add(int num) //since the head of the list is global anyways...
{
    if(start==NULL) //test to see if you're creating the list header
    {
        start = new elem;
        start->key = num;
        start->next = NULL;
    }
    else
    {
        elem* a=start;
        elem* b = new elem;
        while(a->next!=NULL)
            a=a->next; //starts at the head of the list and works its way back
        a->next = b;
        b->key = num;
        b->next = NULL;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void del(int num)
{
    elem* a = start;
    elem* b;
    while(a->key!=num) //find the list element you're looking for
        a=a->next; 
    if(a==start) //test if you need to change the list header
    {
        b=start->next;
        delete start;
        start = b;
    }
    else
    {
        b=a->next;
        a->next=b->next;
        delete b;
    }
}

The arranging is a bit of a pain and I can't be bothered to work out a more efficient algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void arrange(void)
{
    elem* a, *b;
    int i=0;
    while(!i) //keep going through the list over and over again until no elements are changed in an entire run
    {
        i=1;
        a=start;
        while(a!=NULL) //until the end of the list
        {
            b=a->next;
            if(b->key<a->key)
            {
                a->next=b->next;
                b=a;
                if(a==start)
                    start=b;
                i=0;
            }
            a=a->next;
        }
    }
}

I'm especially unsure about that last one, but it should do the trick. I think.
Last edited on
thanks for your help
Topic archived. No new replies allowed.