Skip to content

Commit

Permalink
单向链表的基本操作(指针版)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-XYS authored Nov 28, 2016
1 parent 022fe98 commit c84c721
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions Singly-Linked-List(Pointer).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <cstdio>

#define NIL 0

using namespace std;

struct node
{
int id, value;
node *next;
};

node *head;

void insert(int id, int value)
{
node *o = new node;
o->next = head;
head = o;
o->id = id;
o->value = value;
}

int find(int id)
{
node *cur = head;
while (cur != NIL)
{
if (cur->id == id)
{
return cur->value;
}
cur = cur->next;
}
return -1;
}

bool del(int id)
{
if (head == NIL)
{
return false;
}
if (head->id == id)
{
node *temp = head;
head = head->next;
delete temp;
return true;
}
node *prev = head, *cur = head->next;
while (cur != NIL)
{
if (cur->id == id)
{
prev->next = cur->next;
delete cur;
return true;
}
prev = cur;
cur = cur->next;
}
return false;
}

int main()
{
int in, id, value;
while (true)
{
scanf("%d", &in);
if (in == 1)
{
scanf("%d%d", &id, &value);
insert(id, value);
}
else if (in == 2)
{
scanf("%d", &id);
printf("%d\n", find(id));
}
else if (in == 3)
{
scanf("%d", &id);
printf("%s\n", del(id) == true ? "deleted" : "failed");
}
else if (in == 0)
{
return 0;
}
else
{
printf("No such command!\n");
}
}
return 0;
}

0 comments on commit c84c721

Please sign in to comment.