程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c語言 鏈表-單鏈表的基本操作 c語言

c語言 鏈表-單鏈表的基本操作 c語言

編輯:編程解疑
單鏈表的基本操作 c語言

用C語言實現單鏈表的各種基本操作

最佳回答:


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;
}
看不懂的話可以私我

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved