函数功能: 链表的创建 打印,插入、删除、逆序
主要出现的问题有:
1、在创建链表时,使用了:
head = NULL;
p = head;
然后让P向前走,最后得到的结果始终是head = NULL;主要原因是此时head还没有赋值,指向一个NULL,p=head之后,p指针动并不表示head也向前,因为此时两个指针并不是指向同一处的,只是他们都指向NULL而已;
正确的做法应该是:先将head->=(node*)malloc(struct student),然后将p = head;这样就不会出错,或者使用程序中所使用的方法,先将head指针指向一个实指针,然后将P指针指向head指针,这样再移动p指针时,head就会一直指向p指针的那个链表头了;
2 在插入链表是出现的问题和创建链表时想类似;主要思想家就是先判断要插在什么地方,然后取得前后的指针,主要注意在头和尾部的处理,头部插入时,head指针要指向新插入的那个指针;
3 删除链表是出现问题;始终删除不了头指针,最后发现是在main函数中出现了问题;main函数中没有用head=list_delete(head,num),子函数里的head只是作为形参传入,函数调用之后并没有改变;因此需要重新赋值;
4 链表的逆序主要注意的地方是:1、判断head是否为空,2、利用两个指针交替前进,3、最后在head = p 时要注意先将head->next = NULL之后再做操作head = p;
代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
/*******************************************************************************
data: 2012.7.11
fuction :list_creat,list_del.list_invert,list_print;
****************************************************************************/
typedef struct student{
int data;
char name[10];
struct student *next;
}node;
node* list_creat()
{
node *head,*p,*s;
int x ;
int cycle = 1;
head = NULL;
while(cycle){
printf("please input the data\n");
scanf("%d",&x);
if(x != 0){
//printf("the %d's data is : %d\n", cycle, x);
s = (node*)malloc(sizeof(struct student));
s->data = x;
if( head == NULL ){
head = s;
p = head;
} else {
p->next = s;
p = s;
}
cycle++;
}else
break;
//cycle = 0;
}
printf("the data count :%d\n",cycle);
p->next = NULL;
return head;
}
node *list_insert(node *head, int num)
{
node *p, *s,*ptr;
ptr = (node*)malloc(sizeof(struct student));
ptr->data = num;
p = head;
while((ptr->data > p->data) && (p->next != NULL)){
s = p;
p = p->next;
}
if(ptr->data <= p->data){
if (p == head){
ptr->next = p;
head = ptr;
}
else{
s->next = ptr;
ptr->next = p;
}
}
else{
p->next =ptr;
ptr->next = NULL;
}
return head;
}
node *list_delete(node *head, int num)
{
node *p,*s;
p = head;
while((num != p->data) && (p->next != NULL)){
s = p;
p = p->next;
}
if(num == p->data){
if(p == head)
head = p->next;
else
s->next = p->next;
}else
printf("can not find the number %d int the list\n",num);
return head;
}
node *list_invert(node *head)
{
node *p,*s,*q;
p = head;
if(p == NULL){
printf("the list num is NULL!\n");
return head;
}
p = head;
s = p->next;
while(s != NULL){
q = s->next;
s->next = p;
p = s;
s = q;
}
head->next = NULL;
head = p;
return head;
}
int list_print(node *head)
{
node * p;
int i = 1;
p = head;
if(p == NULL)
printf("the list creat is err!\n");
while(p != NULL){
printf("the %d's data is : %d\n", i++, p->data);
p = p->next;
}
return 0;
}
int main()
{
node *head;
// head = (node*)malloc(sizeof(struct student));
int n,m;
int k = 1;
while(k){
printf("1---- creat a new list!\n ");
printf("2---- insert a num into the new list!\n ");
printf("3---- delete a num in the list!\n ");
printf("4-----invert the list\n");
printf("please input the num you want to do!\n");
scanf("%d",&m);
switch(m)
{
case 1:
printf("--------------1 creat a new list!-----------------\n ");
head = list_creat();
list_print(head);
break;
case 2:
printf("------------------2 insert a num into the list-----------\n");
printf("plsease input a data :\n");
scanf("%d",&n);
head = list_insert(head,n);
printf("now,print the list after insert!\n");
list_print(head);
break;
case 3:
printf("---------------3 delete a num in the list!------------------\n ");
printf("please input the num you want to delete!\n");
scanf("%d",&n);
printf("----before the delete the lis is :-------\n");
list_print(head);
head = list_delete(head, n);
printf("--------after the delete the lis is :--------------\n");
list_print(head);
break;
case 4:
printf("---------- 4 invert the list----------------\n");
printf("-------before invert the list num was------\n");
list_print(head);
printf("after the invert,the list was:\n");
head = list_invert(head);
list_print(head);
break;
default:
printf("no input!\n");
k = 0;
break;
}
}
return 0;
}