C语言实现双向链表的基本操作
上一篇 /
下一篇 2010-11-03 12:32:44
#include
#include
#include
#include
typedef struct _Person
{
char name[10];
char phone[12];
struct _Person* prev;
struct _Person* next;}Person;
//初始化
Person* InitList()
{
Person* temp;
temp = (Person*)malloc(sizeof(Person));
if(temp == NULL)
{
return NULL;
}
temp->prev = temp;
temp->next = temp;
return temp; }
//销毁
void UnitList(Person* list)
{
Person* p1;
Person* p2;
p1 = list->next;
while(p1 != list)
{
p2=p1->next ;
free(p1);
p1 = p2;
}free(list);
}
//查看
Person* GetList(Person* list,int nGet)
{
int i=1;
Person* p1;
if(list == NULL)
{
return NULL;
}
p1 = list->next;
while(p1!=NULL&&i
{
p1 = p1->next;
i++;
}
if(p1 == NULL)
{
return NULL;
}
if(i>nGet)
{
return NULL;
}
return p1;
}
//插入
Person* InsertList(Person* list,int nPos,Person* pInsert){
Person* temp=NULL;
Person* temp2=NULL;
Person* temp3 = NULL;
int i =1;
if(list == NULL)
{
return NULL;
}
//空表,插入第一个元素
if(nPos == 1 && list->next == list && temp!=list)
{
//分配内存并拷贝值
temp = (Person*)malloc(sizeof(Person));
if(temp == NULL)
{
return list;
}
strcpy(temp->name,pInsert->name);
strcpy(temp->phone,pInsert->phone);
//修改表关的直接前驱和后继
list->next = temp;
list->prev = temp;
temp->prev = list;
temp->next = list;
return list;
}
temp = list->next;
//找到插入位置的前一个节点
while(temp != NULL && i
{
temp = temp->next;
i++;
}
if(temp == NULL)
{
return list;
}
if( i < nPos-1)
{
return list;
}
//为新的节点分配空间并赋值
temp2 = (Person*)malloc(sizeof(Person));
if(temp2 == NULL)
{
return list;
}
strcpy(temp2->name,pInsert->name);
strcpy(temp2->phone,pInsert->phone);
//找到当前节点
temp3 = temp->next;
//修改前驱后继关系
temp->next = temp2;
temp2->prev = temp;
temp2->next = temp3;
temp3->prev = temp2;
return list; } Person* DeleteList(Person* list,int nPos)
{
Person* temp1=NULL;
Person* temp2=NULL;
Person* temp3 = NULL;
int i =1;
if(list == NULL)
{
return NULL;
}
temp1 = list->next;
//找到插入位置的前一个节点
while(temp1 != NULL && i
{
temp1 = temp1->next;
i++;
}
if(temp1 == NULL)
{
return list;
}
if( i < nPos-1)
{
return list;
}
temp3 = temp1->next;
temp2 = temp1->prev;
temp2->next=temp3;
temp3->prev=temp2;
free(temp1);
temp1 = NULL;
return list;
}
Person* ModifyList(Person* list,int nPos, Person* pModify)
{
Person* p1=NULL;
int i = 1;
if(list == NULL)
{
return NULL;
}
p1 = list->next ;
while(p1 != NULL && i
{
p1 = p1->next ;
i++;
}
if(p1 == NULL)
{
return list;
}
if(i>nPos)
{
return list;}
strcpy(p1->name ,pModify->name);
strcpy(p1->phone ,pModify->phone);
list = p1;
return list;} int main()
{
Person newnode;
int i = 0,n,count=0;
Person* head = NULL;
Person* temp = NULL;
Person* Perget= NULL;
Person Modify={"good","123*9"};
head = InitList();
for(i = 1;i<=20;i++)
{
sprintf(newnode.name,"stud%02d",(21-i));
sprintf(newnode.phone,"1234*%04d",(21-i));
InsertList(head,i+1,&newnode);
}
temp = head->next;
if(NULL == temp)
{
return 0;
}
printf("请输入你要删除的条数:");
scanf("%d",&n);
if(n<20&&n>0)
{
DeleteList(head,n);
}
else
{
printf("此数据不存在!\n");
}
printf("\n插入的数据为:\n");
for(i = 1;temp != head;i++)
{
printf("name = %s phoe = %s\n", temp->name, temp->phone);
count++;
temp = temp->next; }
printf("\n请输入你要查询的条数:");
scanf("%d",&n);
if(n<20&&n>0)
{
Perget=GetList(temp,n);
if(Perget!=NULL)
{
printf("Look Result: name = %s phoe = %s\n", Perget->name, Perget->phone);
}
}
else{
printf("此数据不存在!\n");
}
printf("\n请输入你要修改的条数(修改成 %s,%s):",Modify.name ,Modify.phone);
scanf("%d",&n);
if(n<20&&n>0)
{ Perget=ModifyList(temp,n,&Modify);
if(Perget!=NULL)
{
printf("Insert Result: name = %s phone = %s\n\n", Perget->name, Perget->phone);
}
}
else
{
printf("此数据不存在!\n\n");
}
printf("共输入的条数为:%d\n\n",count);
UnitList(temp);
return 0; }
TAG:
我来说两句
显示全部
内容
昵称
验证
提交评论