Questions about pointers in the linked list

I wrote a code using a linked list to store student information and functions add, delete, search and show it. When receiving a pointer to the first node is created first, and remains empty node address is not stored.
I appreciate your help, why is this happening?
The pointer first should not be null . Must store the address of the first node.


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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include<iostream>
#include<stdlib.h>
#include<string>


using namespace std;
class node{ 
int stnum;
float av;
string name;
public:
friend class linkedlist;
node *next;
};
class linkedlist{
public:
linkedlist(){first=NULL;}
void add();
void del(int);
void show();
void search(int);
private:
node *first;
node *last;
};
void linkedlist::add()
{
	if (first==NULL)
	{
		node *temp=new node;
		temp->next=NULL;
		first =temp;
		cout<<"please enter name , ave, student number:"<<"\n";
		cin>>temp->name>>temp->av>>temp->stnum;
		cout<<"\n";
		last=temp;
	}
	else
	{
        node *ptr=new node;
		cout<<"please enter name , ave, student number:"<<"\n";
		cin>>ptr->name>>ptr->av>>ptr->stnum;
		cout<<"\n";
		ptr->next=NULL;
		last->next=ptr;
		last=ptr;
	}
}
void linkedlist::del(int x)
{
	node *temp,*temp1;
	int sw=0;
	temp=temp1=first;
	if (first->stnum==x)
	{
		temp=first;
		first->next=first;
		delete temp;
		sw=1;
	}
	else
	{
		while (temp!=last)
		{
			temp=temp1->next;
			if(temp->stnum==x)
			{
			if (temp==last)
			{
				temp1->next=NULL;
				delete temp;
				last=temp1;
				sw=1;
				break;
			}
			else
			{
				temp1->next=temp->next;
				delete temp;
				sw=1;
				break;
			}
			}
		}
		temp1=temp1->next;
	}
	if(sw==0)
	{
		cout<<"No student with student number"<<" "<<x<<" "<<" \n";
	}
}
void linkedlist::show()
{
	if (first==NULL)
	{
		cout<<"No student\n";
	}
	else
	{
		node *temp;
		temp=first;
		while (temp!=NULL)
		{
			cout<<temp->name<<temp->stnum<<temp->av<<"\n";
			temp=temp->next;
		}
	}
}
void linkedlist::search(int x)
{
	node *temp,*temp1;
	temp=temp1=first;
	int sw=0;
	while (temp!=last)
	{
		temp=temp1->next;
		if (temp->stnum==x)
		{
			cout<<temp->name<<temp->av<<"\n";
			sw=1;
		}
	}
	temp1=temp1->next;
	if (sw==0)
		{
			cout<<"No student with student number"<<" "<<x<<" "<<" \n";
		}
}
int main(int argc, char* argv[])
{
	int a=0
        while(a==0){
		int s,h,m;
		class linkedlist l;
		cout<<"menu:\n";
		cout<<"add.................1\n";
		cout<<"delete..............2\n";
		cout<<"show................3\n";
		cout<<"search..............4\n";
		cin>>m;
		switch (m)
		{
		case 1:
			{
			    l.add();
			    break;
			}
		case 2:
			{
				cout<<"enter student number : \n";
				cin>>h;
				l.del(h);
				break;
			}
		case 3:
			{
				l.show();
				break;
			}
		case 4:
			{
				cout<<"enter student number : \n";
				cin>>s;
				l.search(s);
					break;
			}

		default:
			break;
		}
}
	
	
	system("PAUSE");
	return 0;
}

Last edited on
I appreciate your help, why is this happening?
The pointer first should not be null . Must store the address of the first node.

You don't do anything but one operation with the list. What makes you think that operation isn't working?
Topic archived. No new replies allowed.