// Sunnyworld.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
typedef struct tagNode
{
int data;
struct tagNode *next;
}node;
node *creat(int n)
{
node *head = NULL;
node *p = NULL;
node *s = NULL;
head = (node*)malloc(sizeof(node));
if (!head)
assert(0);
head->data = 0xFF;
p = head;
for(int i = 1; i <= n; i++)
{
s = (node*)malloc(sizeof(node));
if (!s)
return head;
s->data = i;
s->next = NULL;
p->next = s;
p = s;
}
return head;
}
void DestroyList(node *head)//h
{
node *p = NULL;
int num = 0;
while (head)
{
p = head;
head = head->next;
printf("\r\nfree node%i data = %i", num, p->data);
free( p );
num++;
}
}
void display(node*h)
{
node *p = h->next;
while (p != NULL)
{
printf("\r\n %i", p->data);;
p = p->next;
}
return ;
}
int GetLenth(node *h)
{
node *p = h->next;
int len = 0;
while(p)
{
len++;
p = p->next;
}
return len;
}
int GetElem(node *head, int pos, int *e)
{
node *p = head->next;
int j = 1;
while(p && j < pos)
{
p = p->next;
j++;
}
if (!p || j > pos)
return -1;
*e = p->data;
return 0;
}
int ListInsert(node *head, int pos, int e)
{
node *p = head; int j = 0;
node *s = NULL;
while(p && j < pos - 1)
{
p = p->next;
++j;
}
if (!p || j > pos -1 )
return -1;
s = (node*)malloc(sizeof(node));
if (!s)
return -1;
s->data = e;
s->next = p->next;
p->next = s;
return 0;
}
int ListDelete(node *head, int pos, int *e)
{
node *p = head;
int j = 0;
node *q = NULL;
while(p->next && j < pos -1)//跟插入算法的区别,插入算法只要前驱在就行,删除不仅要前驱在,被删除节点也要存在才行
{
p = p->next;
++j;
}
if (!(p->next) || j > pos - 1) //pos < 1
return -1;
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int ret = 0;
node *head = NULL;
int data = 0;
head = creat(5);
if (!head) return -1;
//ListInsert(head, 1, 11);
int len = GetLenth(head);
//ListInsert(head, len + 2, 22);
//ListDelete(head, 6, &data);
GetElem(head, 1, &data);
printf("\r\n data = %i", data);
display(head);
DestroyList(head);
head = NULL;
display(head);
return 0;
}
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
typedef struct tagNode
{
int data;
struct tagNode *next;
}node;
node *creat(int n)
{
node *head = NULL;
node *p = NULL;
node *s = NULL;
head = (node*)malloc(sizeof(node));
if (!head)
assert(0);
head->data = 0xFF;
p = head;
for(int i = 1; i <= n; i++)
{
s = (node*)malloc(sizeof(node));
if (!s)
return head;
s->data = i;
s->next = NULL;
p->next = s;
p = s;
}
return head;
}
void DestroyList(node *head)//h
{
node *p = NULL;
int num = 0;
while (head)
{
p = head;
head = head->next;
printf("\r\nfree node%i data = %i", num, p->data);
free( p );
num++;
}
}
void display(node*h)
{
node *p = h->next;
while (p != NULL)
{
printf("\r\n %i", p->data);;
p = p->next;
}
return ;
}
int GetLenth(node *h)
{
node *p = h->next;
int len = 0;
while(p)
{
len++;
p = p->next;
}
return len;
}
int GetElem(node *head, int pos, int *e)
{
node *p = head->next;
int j = 1;
while(p && j < pos)
{
p = p->next;
j++;
}
if (!p || j > pos)
return -1;
*e = p->data;
return 0;
}
int ListInsert(node *head, int pos, int e)
{
node *p = head; int j = 0;
node *s = NULL;
while(p && j < pos - 1)
{
p = p->next;
++j;
}
if (!p || j > pos -1 )
return -1;
s = (node*)malloc(sizeof(node));
if (!s)
return -1;
s->data = e;
s->next = p->next;
p->next = s;
return 0;
}
int ListDelete(node *head, int pos, int *e)
{
node *p = head;
int j = 0;
node *q = NULL;
while(p->next && j < pos -1)//跟插入算法的区别,插入算法只要前驱在就行,删除不仅要前驱在,被删除节点也要存在才行
{
p = p->next;
++j;
}
if (!(p->next) || j > pos - 1) //pos < 1
return -1;
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int ret = 0;
node *head = NULL;
int data = 0;
head = creat(5);
if (!head) return -1;
//ListInsert(head, 1, 11);
int len = GetLenth(head);
//ListInsert(head, len + 2, 22);
//ListDelete(head, 6, &data);
GetElem(head, 1, &data);
printf("\r\n data = %i", data);
display(head);
DestroyList(head);
head = NULL;
display(head);
return 0;
}