筆記:
對(duì)頭部操作:
/*************************************************************************
> File Name: head.c
> Author: pengrenchang123
> Mail: 1096429536@qq.com
> Created Time: Wed 21 Dec 2016 09:52:16 AM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h> //malloc free
#include<string.h>
#include<unistd.h> //sleep
typedef struct student{
int ID;
char name[32];
struct student *next;
}STU,*pSTU;
//創(chuàng)建節(jié)點(diǎn)
pSTU create_node(){
pSTU newNode = (pSTU)malloc(sizeof(STU));
if(newNode == NULL)
newNode = (pSTU)malloc(sizeof(STU));
newNode->next = NULL;
printf("創(chuàng)建新節(jié)點(diǎn)成功3せ怼J雇谩豫柬!\n");
return newNode;
}
//隨意添加
pSTU add_random_node(pSTU head,int position){
pSTU p;
pSTU temp = create_node();
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
int i = 0;
for(p = head;p->next != NULL;p = p->next,i++){
if(i == position-1 && p->next != NULL){
temp->next = p->next;
p->next = temp;
temp = NULL;
return head;
}
}
if(i == position-1 && p->next == NULL){
p->next = temp;
temp = NULL;
return head;
}
}
//隨意刪除
pSTU delete_random_node(pSTU head,int position){
pSTU p1 = head;
pSTU p2 = head->next;
int i = 1;
for(;p2 != NULL;p2 = p2->next,p1 = p1->next,i++){
if(position == i){
p1->next = p2->next;
free(p2);
p2 = NULL;
return head;
}
}
if(p2 == NULL && position == i){
p1->next = NULL;
free(p2);
p2 = NULL;
return head;
}
}
//從頭部添加
pSTU add_head_node(pSTU head){
pSTU temp = create_node();
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
temp->next = head->next;
head->next = temp;
temp = NULL;
return head;
}
//顯示鏈表
void show_link(pSTU head){
pSTU p = head->next;
printf("\tID\tname\n");
while(p != NULL){
printf("\t%d\t%s\n",p->ID,p->name);
p = p->next;
}
}
//頭刪
pSTU delete_head_node(pSTU head){
pSTU temp = head->next;
if(temp == NULL){
printf("鏈表無數(shù)據(jù)可刪除:浅俊!\n");
return head;
}
head->next = temp->next;
free(temp);
temp = NULL;
return head;
}
//修改
pSTU modify_node(pSTU head){
char name[32] = {0};
printf("請(qǐng)輸入要修改的姓名:");
scanf("%s",name);
pSTU p;
for(p = head->next;p != NULL;p = p->next){
if(strcmp(name,p->name) == 0){
printf("\t該用戶的信息為:\n");
printf("\tname:%s\n\tID:%d\n",p->name,p->ID);
printf("請(qǐng)輸入用戶的新ID:");
scanf("%d",&p->ID);
return head;
}
}
if(p == NULL)
printf("\t沒有該節(jié)點(diǎn)\n");
return head;
}
//查找節(jié)點(diǎn)舱权,按姓名查找
int find_node(pSTU head){
char name[32] = {0};
printf("請(qǐng)輸入要查找的姓名:");
scanf("%s",name);
pSTU p;
int count = 0;
for(p = head->next;p != NULL;p = p->next){
count++;
if(strcmp(name,p->name) == 0){
printf("查到了節(jié)點(diǎn)\t為第%d個(gè)\n",count);
return count;
}
}
if(NULL == p){
printf("為找到該節(jié)點(diǎn)!!!\n");
return 0;
}
}
//顯示菜單
void menu(pSTU head){
int choice = 0;
while(1){
system("clear");
printf("\t\t1.創(chuàng)建鏈表\n");
printf("\t\t2.添加節(jié)點(diǎn)\n");
printf("\t\t3.刪除鏈表\n");
printf("\t\t4.查看鏈表\n");
printf("\t\t5.修改鏈表\n");
printf("\t\t6.查找鏈表\n");
printf("\t\t7.隨意添加鏈表\n");
printf("\t\t8.隨意刪除鏈表\n");
printf("\t\t0.退出\n");
printf("\t請(qǐng)選擇:");
scanf("%d",&choice);
switch(choice){
case 1:
head = create_node();
break;
case 2:{
int i = 0;
for(i = 0;i<4;i++)
head = add_head_node(head);
break;
}
case 3:
head = delete_head_node(head);
break;
case 4:
show_link(head);
sleep(3);
break;
case 5:
head = modify_node(head);
sleep(2);
break;
case 6:
find_node(head);
sleep(2);
break;
case 7:{
int position = 0;
printf("輸入插入的位置");
scanf("%d",&position);
add_random_node(head,position);
break;
}
case 8:{
int position = 0;
printf("請(qǐng)輸入要?jiǎng)h除的位置");
scanf("%d",&position);
delete_random_node(head,position);
break;
}
case 0:
exit(1);
break;
default:
printf("\t請(qǐng)重新輸入S舛包吝!\n");
break;
}
}
return;
}
//程序入口
int main(int argc,char *argv[]){
pSTU head = create_node();
menu(head);
return 0;
}
對(duì)尾部操作
/*************************************************************************
> File Name: linkTail.c
> Author: pengrenchang123
> Mail: 1096429536@qq.com
> Created Time: Wed 21 Dec 2016 03:05:08 PM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
typedef struct student{
int ID;
char name[32];
struct student *next;
}STU,*pSTU;
pSTU create_link(pSTU head){
if(NULL == head){
head = (pSTU)malloc(sizeof(STU));
head->next = NULL;
}
printf("\t創(chuàng)建鏈表成功\n");
return head;
}
pSTU create_new_node(){
pSTU temp = (pSTU)malloc(sizeof(STU));
if(temp == NULL){
temp = (pSTU)malloc(sizeof(STU));
}
temp->next = NULL;
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
return temp;
}
pSTU add_tail_node(pSTU head){
pSTU temp = create_new_node();
pSTU p = head;
while(p->next != NULL){
p = p->next;
}
p->next = temp;
temp = NULL;
return head;
}
pSTU delete_tail_node(pSTU head){
pSTU p = head;
if(p->next == NULL){
printf("\tlink is empty!!!\n");
return head;
}
for(;p->next->next != NULL;p = p->next);
free(p->next);
p->next = NULL;
return head;
}
void show_link(pSTU head){
pSTU p = head->next;
printf("\t\tID\tname\n");
while(p != NULL){
printf("\t\t%d\t%s\n",p->ID,p->name);
p = p->next;
}
}
void menu(){
pSTU head;
int choice = 0;
system("clear");
while(1){
printf("\t\t1.創(chuàng)建鏈表\n");
printf("\t\t2.添加鏈表\n");
printf("\t\t3.尾部添加鏈表\n");
printf("\t\t4.尾部刪除鏈表\n");
printf("\t\t5.顯示鏈表\n");
printf("\t\t0.退出\n");
printf("\t請(qǐng)選擇:");
scanf("%d",&choice);
switch(choice){
case 1:
head = create_link(head);
break;
case 2:{
int i = 0;
for(i = 0;i<4;i++)
head = add_tail_node(head);
break;
}
case 3:
head = add_tail_node(head);
break;
case 4:
head = delete_tail_node(head);
break;
case 5:
show_link(head);
// sleep(1);
break;
case 0:
exit(1);
break;
default:
printf("請(qǐng)重新輸入W队唷腹纳!");
break;
}
}
}
int main(int argc,char *argv[]){
menu();
return 0;
}
雙頭鏈表
/*************************************************************************
> File Name: doubleHead.c
> Author: pengrenchang123
> Mail: 1096429536@qq.com
> Created Time: Wed 21 Dec 2016 07:08:38 PM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
typedef struct student{
int ID;
char name[32];
struct student *pre;
struct student *next;
}STU,*pSTU;
pSTU create_node(){
pSTU node = (pSTU)malloc(sizeof(STU));
if(node == NULL)
node = (pSTU)malloc(sizeof(STU));
node->next == NULL;
node->pre == NULL;
printf("input ID name:");
scanf("%d %s",&node->ID,node->name);
return node;
}
//頭插
pSTU add_head_doulist(pSTU head){
pSTU temp = create_node();
if(head == NULL){
head->next = temp;
temp->pre = head;
return head;
}
else{
temp->next = head->next;
temp->pre = head;
head->next = temp;
head->next->pre = temp;
return head;
}
}
//頭刪
pSTU delete_head_node(pSTU head){
pSTU p = head->next;
if(p == NULL){
printf("list is empty!\n");
return head;
}else{
head->next = p->next;
p->next->pre = head;
free(p);
p->next = NULL;
p->pre = NULL;
return head;
}
}
//尾插
pSTU add_tail_dounode(pSTU head){
pSTU p = head;
pSTU temp = create_node();
while(p->next != NULL)
p = p->next;
p->next = temp;
temp->pre = p;
temp = NULL;
return head;
}
//尾刪
pSTU delete_tail_dounode(pSTU head)
{
pSTU p = head;
if(head->next == NULL)
{
printf("link is empty!!!\n");
return head;
}
for(;p->next != NULL;p = p->next);
p = p->pre;
free(p->next);
p->next = NULL;
//p->next->pre == NULL;
return head;
}
void show_doulist(pSTU head){
pSTU p = head->next;
printf("\tID\tname\n");
while(p != NULL){
printf("\t%d\t%s\n",p->ID,p->name);
p = p->next;
}
}
int main(){
pSTU head;
head = (pSTU)malloc(sizeof(STU));
head->pre = NULL;
head->next = NULL;
int i = 0;
for(;i<3;i++)
// head = add_head_doulist(head);
// show_doulist(head);
// head = delete_head_node(head);
// show_doulist(head);
head = add_tail_dounode(head);
show_doulist(head);
head = delete_tail_dounode(head);
head = delete_tail_dounode(head);
show_doulist(head);
return 0;
}