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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include "stdafx.h"
#include<iostream>
struct node{
int data;
node *next;
};
void initNode(struct node *head,int nodeData)
{
head->data = nodeData;
head->next = NULL;
}
void addNode(struct node *head,int newdata)
{
using namespace std;
node *current = head;
while(current->next !=NULL)
{
current = current->next;
}
if (current->next == NULL)
{
node *newNode = new node;
current->next = newNode;
newNode->data = newdata;
newNode->next = NULL;
}
}
void insertFront(struct node **head,int frontData)
{
node *frontNode = new node;
frontNode->next = *head;
frontNode->data = frontData;
*head = frontNode;
}
node *searchNode(struct node *head,int key)
{
node *cur = head;
node *nodeFound = cur;
using namespace std;
while(cur)
{
if (cur->data == key)
{
nodeFound = cur;
return nodeFound;
}
cur = cur->next;
}
return nodeFound;
}
bool deleteNode(struct node **head, struct node *delptr)
{
node *cur = *head;
node *tempNextptr= cur;
using namespace std;
while(cur)
{
if (cur == delptr)
{
tempNextptr->next = delptr->next;
if(cur == *head)
*head = cur->next;
delete cur;
return true;
}
tempNextptr = cur;
cur = cur->next;
}
return false;
}
void reverse(struct node **head )
{
node *cur = *head;
node *nxtptr = *head;
node *tempcur = *head;
while(cur)
{
if(cur->next == NULL)
*head = cur;
else
{
nxtptr = cur->next;
cur->next = tempcur;
}
tempcur = cur;
cur = nxtptr->next;
}
}
struct node *copyLinkedlist(struct node *head)
{
using namespace std;
node *cur = head;
node *newHead;
while(cur)
{
cout<<"cur= "<<cur<<endl;
node *newNode = new node;
if(cur == head)
newHead = newNode;
newNode->data = cur->data;
newNode->next = cur->next;
cur = cur->next;
}
return newHead;
}
/* comparelinklist()
{
}
*/
void deletelinklist(struct node *head)
{
node *cur = head;
node *tmptr;
while (cur)
{
if (cur->next == NULL)
delete cur;
else
{
tmptr = cur->next;
delete cur;
}
cur = tmptr;
}
}
void display(struct node *head)
{
using namespace std;
node *cur = head;
while(cur)
{
cout<<"data = "<< cur->data<< endl;
cur = cur->next;
}
}
int main()
{
using namespace std;
node *head = new node;
node *newList;
struct node *found;
bool remove;
initNode(head,10);
display(head);
addNode(head,20);
display(head);
addNode(head,30);
display(head);
found = searchNode(head,30);
cout<<"search node = "<< found->data<< endl;
remove = deleteNode(&head,found);
//cout<<"head= "<<head<<endl;
display(head);
addNode(head,40);
addNode(head,50);
display(head);
//reverse(&head);
newList = copyLinkedlist(head);
cout<<"newlist"<<endl;
display(newList);
insertFront(&head,60);
display(head);
if(remove)
cout<<"true"<<endl;
else
cout<<"false"<<endl;
cin.clear();
cin.ignore(255,'/n');
cin.get();
return 0;
}
|