#include<iostream.h> #include<conio.h> #include<stdlib.h> class node { friend class linkList; char name[20]; int stno; float ave; node *next; }; //************************************ class linkList{ public: linkList(); ~linkList(); void addNode(); void delNode(int &); void display(); private: node *first; node *last; }; //************************************ linkList::linkList() { first=last=NULL; } //********************************* linkList::~linkList() { node *curPtr=first; node *temp; while(curPtr){ temp=curPtr; curPtr=curPtr->next; delete temp; } } //************************************************88 void linkList::delNode(int &no) { node *curPtr=first,*nextPtr=first; while(nextPtr){ if(no==nextPtr->stno) if(nextPtr==first){ first=first->next; delete nextPtr; break; } else { if(nextPtr==last) last=curPtr; curPtr->next=nextPtr->next; delete nextPtr; break; } else{ curPtr=nextPtr; nextPtr=nextPtr->next; } } } //*******************************************************8 void linkList::addNode() { node *newPtr=new node; if(!newPtr){cout<<"not allocation;",getch();} newPtr->next=NULL; clrscr(); cout<<"Name "<<"stno "<<" aver\n"; gotoxy(1,2); cin>>newPtr->name; gotoxy(20,2); cin>>newPtr->stno; gotoxy(30,2); cin>>newPtr->ave; if(first==NULL) first=last=newPtr; else{ last->next=newPtr; last=newPtr; } } void linkList::display() { int r=2; clrscr(); cout<<"name "<<"sn "<<" avra\n"; node *curPtr=first; while(curPtr){ gotoxy(1,r); cout<<curPtr->name<<" "; gotoxy(20,r); cout<<curPtr->stno<<" "; gotoxy(30,r); cout<<curPtr->ave<<" "; curPtr=curPtr->next; r++; } } //888888888888888888888888888888888888888888888888888888 int menu(); int main() { int no; linkList testList; for(;;){ clrscr(); switch(menu()){ case 1:testList.addNode();break; case 2: cout<<"enter stno to delet"; cin>>no; testList.delNode(no);break; case 3: testList.display();getch();break; case 4: exit(1); } } } int menu() { int choice; cout<<"1 enter a studn\n" <<"2 delet \n" <<"3 disp \n" <<"4 end prog\n"; cout<<"\n enter your select (1-4)"; cin>>choice; return choice; }
/* #include <iostream> using namespace std; struct NodeType { int data; NodeType *next; }; class linklist { private: NodeType *first; NodeType *last; public: linklist() { first = new NodeType(); last = new NodeType(); first = last = NULL; } ~linklist(); void insertback(); void insertforward(); void delnode(int); // delete int from linklist void showlist(); //print linklist // void addnodeback(int,int); //insert 1 befor 2 // void sortlink(); //sort lnklist }; linklist::~linklist() { NodeType *temp, *current = first; while (current) { temp = current; current = current->next; delete temp; } first = NULL; } void linklist::insertback() { int num; cout << "enter num. end with -999"; cin >> num; while (num != (-999)) { NodeType *newnode; newnode = new NodeType(); newnode->next = NULL; newnode->data = num; if (first == NULL) { first = newnode; last = newnode; } else { newnode->next = first; first = newnode; } cin >> num; } } void linklist::insertforward() { int num; cout << "enter number. end with -999"; cin >> num; while (num != -999) { NodeType *newnode; newnode = new NodeType(); newnode ->next = NULL; newnode ->data = num; if (first == NULL) { first = newnode; last = newnode; } else { last->next = newnode; last = newnode; } cin >> num; } } // for print the list void linklist::showlist() { if (first == NULL) { cout << "list is empty."; return; } NodeType *current; current = new NodeType(); current = first; while (current != NULL) { cout << current->data << " "; current = current->next; } } //for delete a from the listnode void linklist::delnode(int x) { if (first == NULL) { cout << "list is empty."; return; } if (first->next == NULL) { NodeType *temp, *current = first; while (current) { temp = current; current = current->next; delete temp; } first = NULL; return; } NodeType *current = new NodeType(); current = first; while (current) { if (current->next->data == x) { NodeType *p = new NodeType(); p->next = current->next; current->next = current->next->next; delete p; return; } current = current->next; } cout << "\n" << x << "deleted.\n"; } int main() { linklist n; int a; n.insertforward(); cout << "\n\n\n"; n.showlist(); cout << "\ndelete :"; cin >> a; n.delnode(a); cout << "\n\n\n"; n.showlist(); return 0; }