数据结构:
四种逻辑结构:集合结构、线性结构、树型结构、图形结构
四种存储结构:顺序存储、链式存储、索引存储、散列存储
链表在数据结构中是个难点也是重点
在链表中一个数据元素由两部分构成,数据内容与关系。
关系:通过指针元素来表示,存储下一个数据元素的地址
下面是关于链表的操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[20];
struct node * next;
};
struct node * create()
{
struct node * head = malloc(sizeof(struct node));
if(head == NULL)
{
printf("malloc linklist error\n");
return NULL;
}
head->next = NULL;
return head;
}
void insert_list(struct node * head,int pos,char * data)
{
struct node * p = head;
int i = 0;
while(i < pos - 1 && p->next != NULL)
{
p = p->next;
i++;
}
struct node * new = malloc(sizeof(struct node ));
new->next = p->next;
p->next = new;
strcpy(new->name,data);
}
int empty(struct node * head)
{
if(head->next == NULL)
{
printf("is empty\n");
return 1;
}
return 0;
}
void delete_list(struct node * head,int pos)
{
if(empty(head))
{
return ;
}
struct node * p = head;
int i = 0;
while(i < pos - 1 && p->next != NULL)
{
p = p->next;
i++;
}
if(p->next == NULL)
{
printf("pos is not found\n");
return ;
}
struct node * q = p->next;
p->next = q->next;
printf("delete data is : %s\n",q->name);
free(q);
}
void update_pos_list(struct node * head,int pos ,char * newdata)
{
struct node * p = head;
int i = 0;
while(i < pos)
{
p = p->next;
if(p == NULL)
{
printf("error pos\n");
return ;
}
i++;
}
strcpy(p->name,newdata);
}
void update_data_list(struct node * head,char * data,char * newdata)
{
/*
struct node * p = head->next;
while(p != NULL)
{
if( strcmp(p->name,data) == 0 )
{
strcpy(p->name,newdata);
}
p = p->next;
}
*/
struct node * p = head;
while(p->next != NULL)
{
p = p->next;
if( strcmp(p->name,data) == 0 )
{
strcpy(p->name,newdata);
}
}
}
void show_list(struct node * head)
{
struct node * p = head;
while(p->next != NULL)
{
p = p->next;
printf("%s\t",p->name);
}
printf("\n");
}