#include "stdio.h"
#include "stdlib.h"
struct list
{
int num;
struct list *next;
};
typedef struct list node;
typedef node *link;
link createlist(int *array,int len)
{
link head;
link ptr,ptr1;
int i;
head = (link)malloc(sizeof(node));
if(!head)
return NULL;
head->num = array[0];
head->next = NULL;
ptr = head;
for (i=1;i<len;i++)
{
ptr1 = (link)malloc(sizeof(node));
if(!ptr1)
return NULL;
ptr1->num = array[i];
ptr1->next = NULL;
ptr->next = ptr1;
ptr = ptr->next;
}
return head;
}
void printlist(link ptr)
{
while(ptr)
{
printf("[%d]",ptr->num);
ptr = ptr->next;
}
printf("\n");
}
link findnode(link head,int num)
{
link ptr;
ptr = head;
while(ptr)
{
if (ptr->num == num)
return ptr;
ptr = ptr->next;
}
return ptr;
}
link deletelist(link head,link ptr)
{
link previous;
//previous = head;
if (head == ptr)
return head->next;
else
{
previous = head;
while(previous->next != ptr)
previous = previous->next;
if (ptr->next == NULL)
{
previous->next = NULL;
}
else
previous->next = ptr->next;
}
free(ptr);
return head;
}
void freelist(link head)
{
link ptr;
while(head)
{
ptr = head;
head = head->next;
free(ptr);
}
}
void main()
{
int list[6] = {1,2,3,4,5,6};
link head;
link ptr;
int num;
head = createlist(list,6);
if(!head)
{
printf("内存分配失败!\n");
exit(1);
}
printf("原来链表:");
printlist(head);
while(1)
{
printf("要输入要删除的邮箱号码:");
scanf("%d",&num);
if(num != -1)
{
/*ptr = head;
while(ptr)
{
ptr = findnode(head,num);
head = deletelist(head,ptr);
freelist(head);
printf("删除后的邮箱号码:");
printlist(head);
}*/
ptr = findnode(head,num);
if(!ptr)
printf("没有找到!\n");
else
{
head = deletelist(head,ptr);
printf("删除后的链表:");
printlist(head);
}
}
else
exit(1);
}
freelist(head);
}
#include "stdlib.h"
struct list
{
int num;
struct list *next;
};
typedef struct list node;
typedef node *link;
link createlist(int *array,int len)
{
link head;
link ptr,ptr1;
int i;
head = (link)malloc(sizeof(node));
if(!head)
return NULL;
head->num = array[0];
head->next = NULL;
ptr = head;
for (i=1;i<len;i++)
{
ptr1 = (link)malloc(sizeof(node));
if(!ptr1)
return NULL;
ptr1->num = array[i];
ptr1->next = NULL;
ptr->next = ptr1;
ptr = ptr->next;
}
return head;
}
void printlist(link ptr)
{
while(ptr)
{
printf("[%d]",ptr->num);
ptr = ptr->next;
}
printf("\n");
}
link findnode(link head,int num)
{
link ptr;
ptr = head;
while(ptr)
{
if (ptr->num == num)
return ptr;
ptr = ptr->next;
}
return ptr;
}
link deletelist(link head,link ptr)
{
link previous;
//previous = head;
if (head == ptr)
return head->next;
else
{
previous = head;
while(previous->next != ptr)
previous = previous->next;
if (ptr->next == NULL)
{
previous->next = NULL;
}
else
previous->next = ptr->next;
}
free(ptr);
return head;
}
void freelist(link head)
{
link ptr;
while(head)
{
ptr = head;
head = head->next;
free(ptr);
}
}
void main()
{
int list[6] = {1,2,3,4,5,6};
link head;
link ptr;
int num;
head = createlist(list,6);
if(!head)
{
printf("内存分配失败!\n");
exit(1);
}
printf("原来链表:");
printlist(head);
while(1)
{
printf("要输入要删除的邮箱号码:");
scanf("%d",&num);
if(num != -1)
{
/*ptr = head;
while(ptr)
{
ptr = findnode(head,num);
head = deletelist(head,ptr);
freelist(head);
printf("删除后的邮箱号码:");
printlist(head);
}*/
ptr = findnode(head,num);
if(!ptr)
printf("没有找到!\n");
else
{
head = deletelist(head,ptr);
printf("删除后的链表:");
printlist(head);
}
}
else
exit(1);
}
freelist(head);
}