单链表的基本操作 c语言

用C语言实现单链表的各种基本操作

c

2个回答

typedef struct node
{
int n;
struct node next;
}Node;
Node *create();
void print(Node *head);
Node *sort(Node *head);
Node *swap(Node *head);
void main()
{
Node *head = create();
print(head);
// head = sort(head);
// print(head);
//head = swap(head);
//print(head);
}
Node *create()
{
Node *head=NULL,*p1=NULL,*p2=NULL;
p1 = (Node
)malloc(sizeof(Node));
p1->next = NULL;
while(1 == scanf("%d",&p1->n))
{
if(head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
p1->next = NULL;
}
free(p1);
p1 = NULL;
return head;
}
void print(Node *head)
{
Node *p = head;
while(p != NULL)
{
printf("%d ",p->n);
p = p->next;
}
printf("\n");
}
Node *sort(Node *head)
{
Node *newhead=NULL,*newtail=NULL,*p,*min,*minf;
if(head == NULL || head->next == NULL)
return head;
while(head != NULL)
{
p = min = head;
for(;p->next != NULL;p = p->next)
{
if(p->next->n < min->n)
{
min = p->next;
minf = p;
}
}
//2
if(newhead==NULL)
{
newhead = newtail = min;
}
else
{
newtail->next = min;
newtail = min;
}
//3
if(min == head)
head = head->next;
else
minf->next = min->next;
}
if(newhead != NULL)
newtail->next = NULL;
return newhead;
}
Node *swap(Node *head)
{
Node *p = NULL,*pf = NULL,*pn = NULL;
if(head == NULL || head->next == NULL)
return head;
pf = head;
p = pf->next;
pf->next = NULL;
while(p->next != NULL)
{
pn = p->next;
p->next = pf;
pf = p;
p = pn;
}
p->next = pf;
head = p;
return head;
}

void test(Node *head)
{
Node *p1,*p2;
p1 = p2 = head;
while(p1 != NULL)
{
p1 = p1->next;
if(p1!=NULL)
{
p1 = p1->next;
p2 = p2->next;
}
}
}

#endif

typedef struct data
{
int n;
struct data next;
struct data *front;
}Node;
#if 0
Node *create()
{
Node *head=NULL,*p1=NULL,*p2=NULL;
p1 = (Node
)malloc(sizeof(Node));
p1->next = NULL;
while(1 == scanf("%d",&p1->n))
{
if(head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
p1->next = NULL;
}
free(p1);
p1 = NULL;
p2->next = head;
return head;
}
void print(Node *head)
{
Node *p = head;
do
{
printf("%d ",p->n);
p = p->next;
}while(p != head);
printf("\n");
}
#endif

Node create()
{
Node *head=NULL,*p1=NULL,*p2=NULL;
p1 = (Node
)malloc(sizeof(Node));
p1->next = NULL;
p1->front = NULL;
while(1 == scanf("%d",&p1->n))
{
if(head == NULL)
head = p1;
else
{
p2->next = p1;
p1->front = p2;
}
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
p1->next = NULL;
p1->front = NULL;
}
free(p1);
p1 = NULL;
return head;
}
void print(Node *head)
{
Node *p = head,*p1 = NULL;
while(p != NULL)
{
p1= p;
printf("%d ",p->n);
p = p->next;
}
printf("\n");
while(p1 != NULL)
{
printf("%d ",p1->n);
p1 = p1->front;
}
printf("\n");
}
int main()
{
Node *head = create();
print(head);
return 0;
}
看不懂的话可以私我

github_36186488
Findway_ 我靠。。。
接近 3 年之前 回复

 1、查找

  对单链表进行查找的思路为:对单链表的结点依次扫描,检测其数据域是否是我们所要查好的值,若是返回该结点的指针,否则返回null

  因为在单链表的链域中包含了后继结点的存储地址,所以当我们实现的时候,只要知道该单链表的头指针,即可依次对每个结点的数据域进行检测

  以下是应用查找算法的一个例子:

#include <stdio.h>
#include <malloc.h>
#include <string.h> /*包含一些字符串处理函数的头文件*/
#define n 10
typedef struct node
{
 char name[20];
 struct node *link;
}stud;

stud * creat(int n) /*建立链表的函数*/
{
 stud *p,*h,*s;
 int i;
 if((h=(stud *)malloc(sizeof(stud)))==null)
 {
  printf(\"不能分配内存空间!\");
  exit(0);
 }
 h->name[0]=\'\0\';
 h->link=null;
 p=h;
 for(i=0;i<n;i++)
 {
  if((s= (stud *) malloc(sizeof(stud)))==null)
  {
   printf(\"不能分配内存空间!\");
   exit(0);
  }
  p->link=s;
  printf(\"请输入第%d个人的姓名\",i+1);
  scanf(\"%s\",s->name);
  s->link=null;
  p=s;
 }
 return(h);
}

stud * search(stud h,char *x) /查找链表的函数,其中h指针是链表的表头指针,x指针是要查找的人的姓名*/
{
 stud p; /当前指针,指向要与所查找的姓名比较的结点*/
 char y; /保存结点数据域内姓名的指针*/
 p=h->link;
 while(p!=null)
 {
  y=p->name;
  if(strcmp(y,x)==0) /*把数据域里的姓名与所要查找的姓名比较,若相同则返回0,即条件成立*/
   return(p); /*返回与所要查找结点的地址*/
  else p=p->link;
 }
 if(p==null)
  printf(\"没有查找到该数据!\");
}

main()
{
 int number;
 char fullname[20];
 stud head,*searchpoint; /*head是表头指针,searchpoint是保存符合条件的结点地址的指针/
 number=n;
 head=creat(number);
 printf(\"请输入你要查找的人的姓名:\");
 scanf(\"%s\",fullname);
 searchpoint=search(head,fullname); /*调用查找函数,并把结果赋给searchpoint指针*/
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!